gpt4 book ai didi

android - 在 Lollipop 中从远程 BLE 设备读取超过 20 个字节失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:03:45 26 4
gpt4 key购买 nike

我们在从远程 BLE 设备读取特征时遇到一个问题。

此问题发生在 Android OS 5.0 及更高版本中。

以下是产生问题的要点:

  1. Make one peripheral device with one service and one characteristics.
  2. Characteristics will have only read permission. Now set the value of this characteristics with more than 20 characters i.e. 20 bytes.
  3. Now let peripheral device broadcast itself with one service and one characteristics.
  4. Now launch any BLE scanner app from market and connect with this peripheral device.
  5. Once successfully connected with peripheral device just try to read characteristics.
  6. In this case it will not show any data and when debugging the app it show that it returns null data.
  7. The above same case not working in the Android OS 5.0 and above.
  8. Same case working in android 4.4.

因此在 Android OS 5.0 及更高版本中发生了一些变化,在内部禁用了可以读取超过 20 个字符的数据的 readblob() 请求。

最佳答案

这可以通过将您的数据拆分为 20 字节的数据包并在发送每个数据包之间实现一个短延迟(即使用 sleep())来简单地实现。

您可以使用 BluetoothGatt.requestMtu()。查看Official document of BluetoothGatt.requestMtu

     Request an MTU size used for a given connection. 
When performing a write request operation (write without response), the data
sent is truncated to the MTU size. This function may be used to request a larger MTU size to be able to send more data at once.

A onMtuChanged(BluetoothGatt, int, int) callback will indicate whether this operation was successful.

Requires BLUETOOTH permission.

如果你想发送更多的 20 个字节,你应该定义数组 byte[] 包括你想要的数据包数量。有一个例子 Android: Sending data >20 bytes by BLE

还有另一个例子How to send more than 20 bytes data over ble in android?

关于android - 在 Lollipop 中从远程 BLE 设备读取超过 20 个字节失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35772118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com