skyin
skyin

Reputation: 578

samsung ble api cannot get notification from multiple GATT characteristics

I am developing an app on Samsung ACE3 to connect Bluetooth Low Energy device. Since Samsung do not want ACE3 to upgrade to Android 4.3, I need to use Samsung ble api. Currently, connection, reading data, sending data and getting notification from one characteristic is all ok. But, when I enable notification for multiple characteristics, then only the first enabled characteristics is able to get notification. Anyone has the same problem? Appreciate your help!

Following code is enable notification on connection

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

Following is the enableNotification method:

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

Upvotes: 4

Views: 1993

Answers (1)

skyin
skyin

Reputation: 578

Just realized the question was addressed in the second answer by miznick in this post. Mainly, it is because Samsung BLE Api behaves synchronously. Basically, the api can only process 1 Gatt instruction, such as write/read characteristics, w/r descriptor and so on, at a time. By calling the w/r GATT method, it is just like to append the Gatt instruction to the system waiting for executing. After executed, system will evoke a related callback method, such as onCharacterWrite, OnDescriptorWrite and so on. Only on or after this point, should we call another Gatt w/r method. The code is given in miznick's post.

This post can also be useful in understanding the behaviour of Samsung Ble api. Do have a look at the Guide and Hint in Samsung BLE official site.

Upvotes: 3

Related Questions