Как подписаться на несколько характеристик BluetoothLE с Android

Я разрабатываю приложение для Android, которое должно подписываться на несколько характеристик BLE.

Но что бы я ни делал, я получаю только обновленные значения одной характеристики.

Вот код:

BluetoothGattCharacteristic characteristicVel = gatt.getService(BleDefinedUUIDs.Service.KOMMMODUL_SERVICE).getCharacteristic(BleDefinedUUIDs.Characteristic.VELOCITY);
                gatt.setCharacteristicNotification(characteristicVel, true);
                BluetoothGattDescriptor descriptorVel = characteristicVel.getDescriptor(
                        BleDefinedUUIDs.Descriptor.CHAR_CLIENT_CONFIG);
                descriptorVel.setValue(BleDefinedUUIDs.Descriptor.ENABLE_NOTIFICATION_VALUE);
                gatt.writeDescriptor(descriptorVel);

            BluetoothGattCharacteristic characteristicAcc = gatt.getService(BleDefinedUUIDs.Service.KOMMMODUL_SERVICE).getCharacteristic(BleDefinedUUIDs.Characteristic.ACCELERATION);
            gatt.setCharacteristicNotification(characteristicAcc, true);
            BluetoothGattDescriptor descriptorAcc = characteristicAcc.getDescriptor(
                    BleDefinedUUIDs.Descriptor.CHAR_CLIENT_CONFIG);
            descriptorAcc.setValue(BleDefinedUUIDs.Descriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(descriptorAcc);

Что бы я ни делал, я получаю только данные о скорости. Если я изменю порядок двух блоков, я получаю только ускорение, но не больше данных о скорости.

Что мне нужно сделать, чтобы подписаться сразу на множество характеристик?

заранее спасибо

Reto


person retokiefer    schedule 06.07.2016    source источник
comment
Не могли бы вы дождаться обратного вызова onDescriptorWrote из первого блока, прежде чем пытаться установить второй?   -  person Zomb    schedule 07.07.2016
comment
Это хороший намек, на самом деле он решил мою проблему. Спасибо большое!   -  person retokiefer    schedule 08.07.2016
comment
Я добавлю его как ответ, чтобы другие люди могли его найти!   -  person Zomb    schedule 08.07.2016


Ответы (2)


Чтобы дескрипторы записывались один за другим, дождитесь обратного вызова от записи дескриптора, прежде чем начинать следующий.

person Zomb    schedule 08.07.2016
comment
Не могли бы вы добавить код для этого? Было бы очень полезно! Спасибо - person Nick; 09.09.2016

Для всех будущих читателей, вот как это сделать:

List<BluetoothGattCharacteristic> characteristics = GetCharacteristicsWithNotifications(gatt);

subscribeToCharacteristics(gatt);

private void subscribeToCharacteristics(BluetoothGatt gatt) {
    if(characteristics.size() == 0) return;

    BluetoothGattCharacteristic characteristic = notifyCharacteristics.get(0);
    gatt.setCharacteristicNotification(characteristic, true);
    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

    UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
    if(descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        gatt.writeDescriptor(descriptor);
    }
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    super.onDescriptorWrite(gatt, descriptor, status);

    characteristics.remove(0);
    subscribeToCharacteristics(gatt);
}
person Davor Zlotrg    schedule 06.11.2017