读取 Lollipop 中远程 BLE 设备超过 20 字节的数据失败

8
我们在从远程BLE设备读取特征时遇到了一个问题。
此问题发生在Android 5.0及以上版本。
以下是生成问题的要点:
制作一个具有一个服务和一个特征的外围设备。
该特征仅具有读取权限。现在将该特征的值设置为超过20个字符,即20字节。
现在让外围设备广播自己一个服务和一个特征。
现在从市场上启动任何BLE扫描应用程序并连接到此外围设备。
一旦成功连接到外围设备,请尝试读取特征。
在这种情况下,它不会显示任何数据,并且在调试应用程序时,它会显示返回空数据。
在Android 5.0及以上版本中,上述相同的情况不起作用。
在Android 4.4中,相同的情况有效。
因此,在Android 5.0及以上版本中存在某些更改,内部禁用了可以读取包含超过20个字符的数据的readblob()请求。

https://dev59.com/SmAf5IYBdhLWcg3w7mU_ - pRaNaY
1个回答

1
这可以通过将数据分成20字节的数据包,并在发送每个数据包之间实施短延迟(即使用sleep())来轻松实现。
您可以使用BluetoothGatt.requestMtu()。请参阅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?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接