安卓:BLE如何读取多个特征?多个、特征、BLE

2023-09-12 11:30:00 作者:青春阿,你太痘了

Android的BLE API的方式来阅读一些特性是异步的,当你要求一定的价值,你的关贸总协定的回调方法被调用。

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.

我不明白如果这样,为什么他们提出的方法异步。如果有人知道它的设计模式,我们应该适应解决这个问题,请大家分享。

I didn't understand if that so why they made methods Async. 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回调

GATT callback

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

可能的解决方案#1

https://$c$c.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.

可能的解决方案#2

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

可能的解决方案#3

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

可能的解决方案#4

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

可能的解决方案#5

http://stackoverflow.com/a/15816566/185022

更新

我设法使其与队列preferabley SynchronousQueue但我会分享我的测试后,最终的解决方案。指定超时否则会卡住,或者如果你要求的特性一些读不支持读取操作。

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

推荐答案

伪code:

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

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

2)将所有你想要阅读的特性

2) insert all the characteristics you want to read

3)打电话给你的requestCharacteristics方法,以便它可以开始消费队列

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

4)从 onCharacteristicsRead 呼叫看到,如果队列的大小大于零的要求多了一个..请务必 PEEK() 从这里

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

5)现在你requestCharacteristics方法做民意调查()并要求BLE关贸总协定的特点。

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

 
精彩推荐