繁体   English   中英

安卓低功耗蓝牙

[英]Bluetooth low energy with android

我正在使用 bbc microbit 微控制器,但在尝试使用 android 从所述设备收集传感器数据时遇到了一些问题。 我需要在两个服务 xyz 轴上读取六个不同的特征。 由于这是一个如此低级的设备,因此没有通知特性,因此我需要不断从设备读取数据以收集数据,但我目前只是获取空值或不正确的值。

 public void setupBluetooth() {

        mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();

        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceaddress);
        mBluetoothGatt = device.connectGatt(this,false,mGattCallback);

        BluetoothGattService accelService = new BluetoothGattService(Accel_Service_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY);

        BluetoothGattCharacteristic accelXChar = new BluetoothGattCharacteristic(Accel_X_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
        BluetoothGattCharacteristic accelYChar = new BluetoothGattCharacteristic(Accel_Y_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
        BluetoothGattCharacteristic accelZChar = new BluetoothGattCharacteristic(Accel_Z_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);

        accelService.addCharacteristic(accelXChar);
        accelService.addCharacteristic(accelYChar);
        accelService.addCharacteristic(accelZChar);

        //Log.d("scan","chars" +accelService.getCharacteristics());

    }
    //get callbacks when something changes
    private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.d("scan","CONNECTED");
                Log.d("scan","gatt device "+gatt.getDevice());
                gatt.discoverServices();

                //BluetoothGattCharacteristic accelXChar = new BluetoothGattCharacteristic(Accel_X_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);

               // Log.d("scan","char "+gatt.readCharacteristic(accelXChar));
               // Log.d("scan","serv "+gatt.getServices());
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            List<BluetoothGattService> services = gatt.getServices();

                gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));



            Log.d("scan","Number of Services "+  services.size());

           /* gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(1));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(2));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(3));*/


            for(int x = 0;x < 4;x++){
                BluetoothGattService[] serviceArray = new BluetoothGattService[4];
                serviceArray[x] = services.get(x);
                Log.d("scan","service  "+ (x + 1) + " " + serviceArray[x].getUuid());
            }

            UUID uuid;
            List<BluetoothGattCharacteristic> gattCharacteristics;
            ArrayList<BluetoothGattCharacteristic> charas;
            for (BluetoothGattService gattService : services) {
                gattCharacteristics = gattService.getCharacteristics();
                charas = new ArrayList<>();
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                        gatt.readCharacteristic(gattCharacteristic);
                    charas.add(gattCharacteristic);
                    uuid = gattCharacteristic.getUuid();
                    //gattCharacteristic.;
                    //Log.d("scan","describe "+ gattCharacteristic.describeContents());
                    Log.d("scan","uuid "+uuid);

                    //setCharacteristicNotification(gattCharacteristic, true);




                    if (uuid.equals(Accel_X_CHAR_UUID)||uuid.equals(Accel_Y_CHAR_UUID)||uuid.equals(Accel_Z_CHAR_UUID)) {
                        Log.d("scan","value of char  " + gattCharacteristic.getValue().toString());


                    }
                }
            }
        }





        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);

            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16,1));
            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,1));
            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,1));

            //Log.d("scan","char read "+characteristic.getValue());
            byte[] bytes = characteristic.getValue();
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
            Log.d("scan","char read "+buffer.getFloat());
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
            super.onCharacteristicChanged(gatt, characteristic);

            Log.d("scan","char changed "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16,0));



            Log.d("scan", "char changed"+ characteristic.getValue());


        }

    };

这种说法“.. 如此低级的设备没有通知特性..”是不正确的。

请查看https://developer.android.com/guide/topics/connectivity/bluetooth-le


private BluetoothGatt bluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
        UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM