首先,
onCharacteristicRead如果您通过以下方式阅读了特征,将触发:
mBluetoothGatt.readCharacteristic(characteristic);
读取特征和设置通知是两件事。您要从中获取数据的特征的类型是什么?
是吗:
- 读
- 通知
- 表明
如果是
read,则可以使用
mBluetoothGatt.readCharacteristic(characteristic);方法读取特征,但如果是
notify或
indicate首先,则必须
descriptor通过调用以下方法来读取特征:
mBluetoothGatt.readDescriptor(ccc);
读取后,它将通过调用
onDescriptorRead回调返回数据。
在这里,您可以通过以下方式通过通知或指示来设置(订阅)特征:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
一旦返回,
true您将需要再次写入描述符(通知或指示的值)
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);// or//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);mBluetoothGatt.writeDescriptor(clientConfig);
完成此操作后,
onCharacteristicChanged每次特性更改时,您都会通过回调获得通知。
您可以在此处阅读更多有关Android上的蓝牙连接
以及在此处了解蓝牙特性的信息



