Android无法订阅服务中的多个特征

时间:2018-03-22 17:42:44

标签: android bluetooth-lowenergy

我正在尝试订阅服务中的多个特征,并获取这两个特征的通知。

这是我的版本1 char:

 @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            List<BluetoothGattCharacteristic> characteristics = gatt.getServices().get(2).getCharacteristics();

            BluetoothGattCharacteristic characteristic = characteristics.get(0);
            gatt.setCharacteristicNotification(characteristic, true);
            characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
            BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptor);


        }




        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            if (characteristic.getUuid().equals(characteristicIDs[0])) {
                Log.i(TAG,"Char for Beacons");
                int threshValue = characteristic.getValue()[0];
                sendBeaconToCloud(threshValue);
            } else if (characteristic.getUuid().equals(characteristicIDs[1])) {
                Log.d(TAG,"Char for Sensors");
                convertSensorValues(characteristic.getValue());
            } else {
                Toast.makeText(BluetoothConnActivity.this,"Unknown char", Toast.LENGTH_SHORT).show();
            }

        }

然后我决定复制onServicesDiscovered中的代码块以及

characterics.get(1)

这不起作用,所以我按照这里指定的方法: How to subscribe to multiple BluetoothLE Characteristics with Android

所以这是生成的代码:

public List<BluetoothGattCharacteristic> chars;
 public BluetoothGatt myGatt;

 @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            chars = gatt.getServices().get(2).getCharacteristics();
            this.myGatt = gatt;
            subscribeToMultiple();

        }

        private void subscribeToMultiple() {
            if (chars.size() == 0) return;

            BluetoothGattCharacteristic characteristic = chars.get(0);
            myGatt.setCharacteristicNotification(characteristic, true);
            characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
            BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            myGatt.writeDescriptor(descriptor);
        }

        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt,descriptor,status);
            chars.remove(0);
            subscribeToMultiple();
        }

但是,我得到输出:

D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000a001-0000-1000-8000-00805f9b34fb enable: true
D/BluetoothGatt: setCharacteristicNotification() - uuid: 0000a002-0000-1000-8000-00805f9b34fb enable: true

但永远不会调用 onCharacteristicChanged

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

首先你可以设置为0.当你收到特征0的onCharacteristicChanged然后取消订阅它并启用特征1.当你收到1取消订阅1并重新启用0.它对我有用。