【问题标题】:Reading More then 20 byte from remote BLE device failed in Lollipop在 Lollipop 中从远程 BLE 设备读取超过 20 个字节失败
【发布时间】:2025-12-25 19:40:12
【问题描述】:

从远程 BLE 设备读取特征时,我们面临一个问题。

此问题发生在Android OS 5.0 及以上。

产生问题的要点如下:

  1. 制作一种具有一种服务和一种特性的外围设备。
  2. Characteristics 将只有读取权限。现在使用超过 20 个字符(即 20 个字节)设置此特性的值。
  3. 现在让外围设备以一种服务和一种特性广播自己。
  4. 现在启动市场上的任何 BLE 扫描仪应用程序并连接此外围设备。
  5. 一旦与外围设备成功连接,只需尝试读取特征。
  6. 在这种情况下,它不会显示任何数据,并且在调试应用程序时会显示它返回空数据。
  7. 上述相同的情况在 Android OS 5.0 及更高版本中不起作用。
  8. 在 android 4.4 中工作的情况相同。

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

【问题讨论】:

标签: android bluetooth bluetooth-lowenergy


【解决方案1】:

这可以通过将数据分成 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?

【讨论】:

    最近更新 更多