我正在开发 BLE 设备。 - 我可以使用 BluetoothGATT 将此设备与应用程序连接 - 我能够在连接到 BluetoothGatt 后第一时间读取所有服务、特征和广告商数据。 - 我们有两个特征,一个需要阅读,另一个需要写作,我可以写出特征。
但问题是我无法在写入后读取特征。 据我了解
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
或
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status)
应该是自动调用的,这些是
的回调方法我的特征写代码是
boolean status = mBluetoothGatt.setCharacteristicNotification(characteristic, state);
我也通过下面的代码启用通知
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
写完后卡在阅读特征部分
任何可理解的帮助,提前感谢亲爱的,我在这一点上被困了 3 天。
最佳答案
onCharacteristicRead() 和 onCharacteristicChanged() 不会自动调用。
onCharacteristicRead 在您调用 characteristic.getValue() 时触发。 像这样:
BluetoothGattCharacteristic charac = ... ;
byte[] messageBytes = charac.getValue();
charac 保存您尝试读取的特征,messageBytes 保存从特征读取的值。
启用通知后将调用 onCharacteristicChanged。看起来您没有完全启用通知。
public static String CLIENT_CONFIGURATION_DESCRIPTOR_STRING = "00002902-0000-1000-8000-00805f9b34fb";
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(CLIENT_CONFIGURATION_DESCRIPTOR_STRING));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothgatt.writeDescriptor(descriptor);
您需要添加一个 writeDescriptor 调用才能成功启用通知。
关于android - 在 BLE Android 中写入后无法获得读取特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350910/