Aeldred
Aeldred

Reputation: 314

BLE readCharacteristics always return false, or getvalue is null

I'm trying to use BLE in Android with a deivice that has fields : battery (RO), status (RO), intensity (R/W). I followed some tutorials about setting up the Gatt with the device. I Use the following code :

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w(LOGGER + mOwner.get().getName(), "onServicesDiscovered received: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
    // Save all services
    for (BluetoothGattService gattService : mBluetoothGatt.getServices()) {
        for (BluetoothGattCharacteristic charac : gattService
                .getCharacteristics()) {
            if (isContained(charac) {
                mCharacteristics.set(mCharacteristicsUuid.indexOf(charac.getUuid()
                        .toString()), charac);
                    mBluetoothGatt.setCharacteristicNotification(charac, true);
                    // UUID for notification
                    BluetoothGattDescriptor descriptor = charac
                            .getDescriptor(UUID.fromString("00002902-0000-1000-" +
                                    "8000-00805f9b34fb"));
                    if (descriptor != null) {
                        descriptor.setValue(BluetoothGattDescriptor
                                .ENABLE_NOTIFICATION_VALUE);
                        mBluetoothGatt.writeDescriptor(descriptor);
                    }
            }
        }
    }

So i try to discover services, if done iterate over them, and their characteristics. If their UUIDs are correct, i add them to mCharacteristics. If this a value readable (here, everyone of them), I enable notification.

When it's done i try to do a first read :

Log.e(LOGGER + mOwner.get().getName(), "Reading first charac(index=0) : " + mBluetoothGatt
            .readCharacteristic(mCharacteristics.get(0)));
}

I have to precise that all of this is inside a dedicated thread, kept up in an Android Service. I'm sure that the device is connected, and characteristics too. For each one i verified the value of (Property & REEADABLE), that always > 0... But for the Read only characteristics i ALWAYS get false on read... And for the intensity (read/write), read returns true, but onCharacteristicRead() is never called, and the getValue() method returns null.

I'm pretty newer using BLE in Android.

Someone has an idea about the problem ? Thanks in advance !

EDIT : I finally found the solution but not as I expected... In fact, if I set the notification enabled, ii can't read the characteristic after... Can someone tell me how i can perform this way :

> 1) on discover
>     a) get all characts + set in list characs I want
>     b) enable notification for ALL of these charac (if possible, I supposed, because of the descriptor that can be null ? )
>     c) first read to know starting values

Upvotes: 0

Views: 4583

Answers (1)

Emil
Emil

Reputation: 18442

You have the answer here: BLE Android, can't enable more than 1 notify on read characteristics.

You have to wait for a GATT operation to complete before you can do another one.

Upvotes: 3

Related Questions