我想订阅多个特征。目前我通过在调用每个writeDescriptor(描述符)之间使用一个计时器来做这个(大多数是成功的)。我想通过使用onDescriptorWrite()回调来做到这一点,但它永远不会被调用。
我有以下回电。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.d("onDescWrite", "yes");
if(characteristics_with_notifications_or_indications.size() > 0) {
characteristics_with_notifications_or_indications.remove(0);
subscribeToCharacteristics();
}
}
};
它已在我的连接功能中初始化。
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
这是我的描述符写
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(CESGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
我想知道我什么时候通过回调完成了writeDescriptor(),所以我可以订阅另一个特性。但是,即使我已成功订阅某个特性并且正在接收有效数据,onDescriptorWrite()仍然无法调用。如何调用onDescriptorWrite()?
答案 0 :(得分:1)
所以我想出了问题所在。回调函数没有被调用,因为我有一个已通知/指示属性的所有特征的列表。但是,在我写入描述符之前,我会检查某些UUID。好吧,我没有检查其中一个特征,因为我没有使用它,它恰好是列表中的第一个!所以,我甚至都不会写入描述符来调用回调函数。