Android 在棒棒糖中从远程BLE设备读取超过20字节失败
当从远程可编程设备读取特征时,我们面临一个问题 此问题发生在Android OS 5.0及以上版本中 产生问题的要点如下:Android 在棒棒糖中从远程BLE设备读取超过20字节失败,android,bluetooth,bluetooth-lowenergy,Android,Bluetooth,Bluetooth Lowenergy,当从远程可编程设备读取特征时,我们面临一个问题 此问题发生在Android OS 5.0及以上版本中 产生问题的要点如下: 使一个外围设备具有一种服务和一种特性 特征将仅具有读取权限。现在将此特征的值设置为超过20个字符,即20个字节 现在,让外围设备以一种服务和一种特性进行自我广播 现在从市场上推出任何BLE扫描仪应用程序,并与此外围设备连接 一旦成功连接到外围设备,只需尝试读取特征 在这种情况下,它不会显示任何数据,在调试应用程序时,它会显示它返回空数据 上述情况在Android OS 5.
因此,Android OS 5.0及更高版本中有一些变化,内部禁用了可以读取超过20个字符的数据的
readblob()
请求。这可以通过将数据拆分为20字节的数据包并在发送每个数据包之间实现一个短延迟(即使用sleep())来实现
您可以使用BluetoothGatt.requestMtu()
。见
如果您想发送更多的20个字节,您应该定义数组字节[],包括您想要的数据包数量。
有一个例子
还有一个例子
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.