Android: BLE how to read multiple Characteristics?
Asked Answered
C

3

22

Android BLE API methods for reading some characteristics are Asynchronous in nature and when you request some value, your GATT callback method is called.

If you request multiple read characteristics values it just simply discards others until it doesn't entertained the first request.

If someone know which design pattern we should adapt to solve this problem please share.

If you want to read some characteristics then you have to request it.

// new value available will be notified in Callback Object
        mBluetoothGatt.readCharacteristic(ch);

GATT callback

public void onCharacteristicRead(BluetoothGatt gatt, android.bluetooth.BluetoothGattCharacteristic characteristic, int status)

Possible Solution # 1

https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained

Anyone can explain how to use this . I think this will help in this case but I am still looking how to use it.

Possible Solution # 2

https://code.google.com/p/mobility-rpc/source/browse/mobility-rpc/trunk/src/main/java/com/googlecode/mobilityrpc/session/impl/MobilitySessionImpl.java#395

Possible Solution # 3

http://tutorials.jenkov.com/java-util-concurrent/synchronousqueue.html

Possible Solution # 4

http://examples.javacodegeeks.com/core-java/util/concurrent/synchronous-queue-example-to-execute-commands/

Possible Solution # 5

https://mcmap.net/q/589387/-wrapping-a-series-of-asynchronous-calls-with-a-synchronous-method-with-a-return-value

Update

I have managed to make it work with a Queue preferabley SynchronousQueue but I'll share my final solution after testing. Specify a timeout otherwise it will get stuck or if you request some read of characteristics that don't support read operation.

You can see which characteristics are readable writeable notifiable see this post

Centrist answered 22/1, 2014 at 9:31 Comment(2)
AFAIK the only solution is to make a queue yourself like what you did. Also have the same question about why making them async.....Casern
Moreover even if write failed, it still returns true. As it doesn't support multiple read/writes. Resulting in a very complex application structure.Centrist
C
14

Pseudocode:

  1. Use a FIFO Queue or Priority Queue depends upon your business logic

  2. insert all the characteristics you want to read

  3. call your requestCharacteristics method so that it can start consuming your Queue

  4. from your onCharacteristicsRead call see if the size of the queue is greater than Zero requests one more.. be sure to peek() from here

  5. now on your requestCharacteristics method do the poll() and request BLE GATT for characteristics.

Centrist answered 26/1, 2014 at 1:19 Comment(0)
S
2

You might have discovered it yourself already, but it might be useful for others.

I didn't understand if that so why they made methods Async.

They probably did it because sending a read-request to another device in synchronous execution would mean you cannot do anything with your app until an answer is received. By making it Async you can do other stuff, but no other bluetooth requests.

Stoichiometric answered 3/8, 2016 at 10:12 Comment(0)
L
0

Possible Solution #6: try the Task feature provided by https://github.com/BoltsFramework/Bolts-Android :-)

Linker answered 3/2, 2015 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.