Android BLE 蓝牙onCharacteristicChanged 方法不调用
最近接了一个新项目,但是对于蓝牙BLE并不大了解。。。这个问题卡我我很久。。一直不知道是哪里错了。onCharacteristicChanged这个方法一直不回调
我的项目需要连接一个蓝牙4.0的设备,用Android BLE 我这边设置了扫描到蓝牙有一个服务,三个特征,分别是 indicate,write,read。我在链接设备后。然后订阅indicate的 characteristic
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
mBluetoothLeService.java
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
}
最后写入在write写入
byte[] data = { (byte) 0xfe, 0x01, 0x00, 0x12, (byte) 0x9c};
writegattCharacteristic.setValue(data);
mBluetoothLeService.writeCharacteristic(writegattCharacteristic);
displayData("send data:" + writegattCharacteristic.getUuid().toString() + " \n data: "
+ Utils.bytesToHexString(data));
Log.w("trySendCmd ", "send data " + Utils.bytesToHexString(data));
但是最后在BluetoothGattCallback 的方法内onCharacteristicChanged 并不回调
麻烦大家帮我看看。。。。谢过了。。。