0
我尝试读取外围设备的特性,但尽管设置了setCharacteristicNotification
,但从不会调用onCharacteristicChanged
。setCharacteristic通知不会触发onCharacteristicChanged
方法,得到我的特点:
private void getCharacteristic(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
for (BluetoothGattService gattService : gattServices) {
if (gattService.getUuid().toString().equals("000018f0-0000-1000-8000-00805f9b34fb")) {
for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) {
if (gattCharacteristic.getUuid().toString().equals("00002af0-0000-1000-8000-00805f9b34fb")) {
mBluetoothLeService.setCharacteristicNotification(gattService.getCharacteristic(gattCharacteristic.getUuid()), true);
}
}
}
}
}
设置通知:
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
}
onCharacteristicChanged
方法,它永远不会触发:
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.d(TAG, "characteristic changed");
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
相同的代码工作正常与不同的外设。
我还从Google Play商店下载了nRF Connect应用程序,该应用程序显示特征,当我在nRF Connect中启用通知时,我的应用程序中的onCharacteristicChanged
开始被调用(我可以看到logcat中的“特性已更改”)。