我正在三星 ACE3 上开发一个应用程序来连接低功耗蓝牙设备。由于三星不希望 ACE3 升级到 Android 4.3,我需要使用三星 ble api。目前,连接、读取数据、发送数据和从一个特征获取通知都可以。但是,当我为多个特征启用通知时,只有第一个启用的特征能够收到通知。有人有同样的问题吗?感谢您的帮助!
以下代码是启用连接通知
if (mBluetoothGatt != null && device != null) {
BluetoothGattService pucService = mBluetoothGatt.getService(device, PROFILE_UART_CONTROL_SERVICE);
if (pucService == null) {
showMessage("PUC service not found!");
return;
}
BluetoothGattCharacteristic motion = pucService.getCharacteristic(MOTION_READ);
if (motion == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, motion);
BluetoothGattCharacteristic voltage = pucService.getCharacteristic(VOLTAGE_READ);
if (voltage == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, voltage);
BluetoothGattCharacteristic pressure = pucService.getCharacteristic(PRESSURE_READ);
if (pressure == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, pressure);
}
enableNotification方法如下:
public boolean enableNotification(boolean enable, BluetoothGattCharacteristic characteristic) {
if (mBluetoothGatt == null)
return false;
if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable))
return false;
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
if (clientConfig == null)
return false;
if (enable) {
Log.i(TAG,"enable notification");
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
Log.i(TAG,"disable notification");
clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
return mBluetoothGatt.writeDescriptor(clientConfig);
}
最佳答案
刚刚意识到这个问题在 miznick 在 this post 的第二个答案中得到了解决。 .主要是因为 Samsung BLE Api 行为同步。基本上,api 一次只能处理 1 个 Gatt 指令,例如写/读特性、w/r 描述符等。通过调用w/r GATT方法,就相当于将Gatt指令追加到等待执行的系统中。执行后系统会调用相关的回调方法,如onCharacterWrite、OnDescriptorWrite等。只有在这一点上或之后,我们才应该调用另一个 Gatt w/r 方法。代码在 miznick 的帖子中给出。
This post也有助于理解 Samsung Ble api 的行为。请务必查看三星 BLE 官方网站中的指南和提示。
关于android - samsung ble api 无法从多个 GATT 特性中获取通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19949638/