7

I am trying to read the characteristics from a BLE device continously .

I have created a Runnable in my service class:

private class BackgroundRunnableForRead implements Runnable
    {


        private volatile  boolean isRunning = true ;
        @Override
        public void run() {
            try {
            BluetoothLeService.this.backgroundRunID = Thread.currentThread().getId();
            while( isRunning) {

                    List<BluetoothGattService> gattServices = BluetoothLeService.this.getSupportedGattServices();

                    if (gattServices != null && gattServices.size() > 0) {
                        BluetoothGattCharacteristic characteristic = getCharacteristic(gattServices);

                        if (characteristic != null && (characteristic.getProperties() & 2) > 0) {
                            BluetoothLeService.this.readCharacteristic(characteristic);
                        }
                    }
                }
            }
            catch(Exception e)
            {
                isRunning= false;
                e.printStackTrace();
            }

        }

        public void kill()
        {
            this.isRunning = false;
        }
    }

And on successful discovery of services i am calling:

public void startReadingCharacteristics()
    {
        System.out.println("BluetoothLeService.startReadingCharacteristics");
        this.mBackgroundRunnable = new BackgroundRunnableForRead();
        mReadThread =  new Thread(mBackgroundRunnable);
        mReadThread.start();

    }

And this is my on characterics read callback -

public void  onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            System.out.println("BluetoothLeService.onCharacteristicRead" + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }

        }

The application works fine on Nexus 5 , Nexus 4 and Motorola G .

When i am running this code on Samsung S6 it does not work, onCharacteristicRead() is not called .

I read that making sequential calls to readCharacteristics() can cause problems as it waits for onCharacteristicRead to execute.

Tim
  • 41,901
  • 18
  • 127
  • 145
Anukool
  • 5,301
  • 8
  • 29
  • 41

1 Answers1

0

It is recommended to execute only one gatt command at a time because commands are not stacked. So you'd have to implement some sort of mechanism that the next read is called after you've got the read callback for the current one.

Keep in mind that gatt callbacks can come from different threads, but this should be no problem if you save the read value in the callback and then trigger the next read from there.

Community
  • 1
  • 1
JPS
  • 604
  • 5
  • 14
  • I tried starting the read of characteristics only when the onCharacteristicRead callback is triggered . Not working though , Do you think if i am doing something wrong somewhere? – Anukool Apr 22 '16 at 09:37
  • well, why should the `onCharacteristicRead` callback be triggered if you not execute the read characteristic command first? – JPS Apr 25 '16 at 08:14
  • First i am executing a read characteristics as soon as the services are discovered , and then for the first time oncharacteristicsRead is called . And after that i call read characteristics in the onCharacteristicsRead it does not work . – Anukool Apr 25 '16 at 08:19
  • You are trying to get the characteristic with `getCharacteristic`, but you pass `gattServices` as a parameter. You should pick the right characteristic from the service running on your device. `gattServices` should be the list with services available. – JPS Apr 25 '16 at 08:56
  • Well it works on all other devices -Moto G , HTC , LG nexus 4 and 5 , Huawei P7 and P8 etc. So i know the logic is correct . Unfortunately i am not able to find out why it does not work on Samsung S5 neo , Samsung note 4 etc. – Anukool Apr 25 '16 at 09:25
  • What happens if you start the read from the ui-thread, does the read callback trigger on your Samsung devices? – JPS Apr 25 '16 at 10:13