我正在尝试订阅 9 个不同的同步特征通知。 我的目标和最小 SDK 版本是 23
我的应用目前最多只能订阅 5 条通知,然后停止。但是,在使用 nRF Connect 应用程序时,我可以手动订阅所有 9 个。
订阅的 5 个特征正确触发了 onCharacteristicChanged()
回调。
这是我设置通知的代码:
numNotifications = 8;
public void setNotify(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
boolean registered = gatt.setCharacteristicNotification(characteristic, true);
if(registered){
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
numNotifications--;
}
在为第 9 个特征调用 setNotify()
后,我一次异步写入描述符一个特征。 (是的,我知道我可以使用循环而不是开关,并尝试使用相同的结果)。
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
// Called when the descriptor is updated for notification
if (status == BluetoothGatt.GATT_SUCCESS) {
switch (numNotifications){
case 7: setNotify(gatt ,chars.get(numNotifications));
break;
case 6: setNotify(gatt ,chars.get(numNotifications));
break;
case 5: setNotify(gatt ,chars.get(numNotifications));
break;
case 4: setNotify(gatt ,chars.get(numNotifications));
break;
case 3: setNotify(gatt ,chars.get(numNotifications));
break;
case 2: setNotify(gatt ,chars.get(numNotifications));
break;
case 1: setNotify(gatt ,chars.get(numNotifications));
break;
case 0: setNotify(gatt ,chars.get(numNotifications));
break;
}
}
}
问题:
谢谢。