retokiefer
retokiefer

Reputation: 109

How to subscribe to multiple BluetoothLE Characteristics with Android

I am developing an Android app which should subscribe to multiple BLE characteristics.

But whatever I do, I receive only the updated values from one characteristic.

Here is the code:

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);

Whatever I do I get only the velocity data. If I change the order of the two blocks I get acceleration only but no more velocity data.

What have I to do in order to subscribe to many characteristics at once?

Thanks in advance

Reto

Upvotes: 3

Views: 2532

Answers (3)

hl x
hl x

Reputation: 1

override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
    if (BleConstant.DESCRIPTOR_UUID == descriptor.uuid.toString().lowercase(Locale.getDefault())) {
        Log.e(TAG,"Descriptor回调状态码:$status")
        if (status == BluetoothGatt.GATT_SUCCESS) {
            //移除第一个特征,开启第二个特征的通知
            characteristicNotify.removeAt(0)
            if(characteristicNotify.size != 0){
                var characteristic = characteristicNotify[0]
                if (!BleHelper.enableIndicateNotification(gatt, characteristic)){
                    gatt.disconnect()
                } else {Log.e(TAG,"2000")}
            }else{
                gatt.apply {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) readPhy()
                    readDescriptor(descriptor)
                    readRemoteRssi() }
            }
        }
    }
}

enableIndicateNotification:

fun enableIndicateNotification(gatt: BluetoothGatt,characteristicUUID:String): Boolean{
    //查找service uuid
    var mService = gatt.getService(UUID.fromString(BleConstant.SERVICE_UUID))

    if(mService == null){
        Log.e(TAG,"service is null")
        gatt.disconnect() //gatt断开连接
        gatt.close() //关闭gatt连接
        return false
    }else {
        Log.e(TAG,"service is connected")
    }
    //订阅特征的通知 并使能 成功onDescriptorWrite将回调
    //查找 characteristic uuid
    var mCharacteristic = mService.getCharacteristic(UUID.fromString(characteristicUUID))

    if(gatt.setCharacteristicNotification(mCharacteristic,true)) 
    //查找descriptor uuid,开启通知
    var descriptor=mCharacteristic.getDescriptor(UUID.fromString(BleConstant.DESCRIPTOR_UUID))
    descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
    if(gatt.writeDescriptor(descriptor)) Log.e(TAG,"descriptor is connected")
    return true
}

Upvotes: -1

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

For all the future readers, here is how to do it:

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);
}

Upvotes: 3

Zomb
Zomb

Reputation: 911

To get descriptors to write one after another, please wait for the callback from a descriptor write before starting the next one.

Upvotes: 3

Related Questions