Android:BLE如何读取多个特征?

AZ_*_*AZ_ 22 android asynchronous bluetooth-lowenergy

用于读取某些特征的Android BLE API方法本质上是异步的,当您请求某些值时,将调用您的GATT回调方法.

如果您请求多个读取特征值,它只会丢弃其他值,直到它不接受第一个请求为止.

我不明白是否这样,为什么他们制作方法Async.如果有人知道我们应该采用哪种设计模式来解决这个问题,请分享.

如果你想阅读一些特征,那么你必须要求它.

// new value available will be notified in Callback Object
        mBluetoothGatt.readCharacteristic(ch);
Run Code Online (Sandbox Code Playgroud)

GATT回调

public void onCharacteristicRead(BluetoothGatt gatt, android.bluetooth.BluetoothGattCharacteristic characteristic, int status)
Run Code Online (Sandbox Code Playgroud)

可能的解决方案#1

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

任何人都可以解释如何使用它.我认为这将有助于这种情况,但我仍然在寻找如何使用它.

可能的解决方案#2

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

可能的解决方案#3

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

可能的解决方案#4

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

可能的解决方案#5

/sf/answers/1107159651/

更新

我已经成功地使用Queue更好的SynchronousQueue,但我会在测试后分享我的最终解决方案.指定超时,否则它将被卡住或者您请求读取不支持读取操作的特性.

您可以看到哪些特性是可读写的,请参阅此帖子

AZ_*_*AZ_ 13

伪代码:

1)使用FIFO队列或优先级队列取决于您的业务逻辑

2)插入您想要阅读的所有特征

3)调用您的requestCharacteristics方法,以便它可以开始使用您的Queue

4)从您的onCharacteristicsRead调用中查看队列的大小是否大于零请求一个..请务必peek()从这里开始

5)现在根据您的poll()要求特征方法执行并请求BLE GATT的特征.