一:业务场景: 蓝牙设备发送超过20个字节 ,手机端接收数据接收不到。硬件不分包发送,搞得有点奇怪,没有超过20个字节的能接收到数据。这是Android 底层的限制。
* Android 底层貌似做了限制只能接受20个字节 * There are four basic operations for moving data in BLE: read, write, notify, and indicate. * The BLE protocol specification requires that the maximum data payload size for these * operations is 20 bytes, or in the case of read operations, 22 bytes. * BLE is built for low power consumption, for infrequent short-burst data transmissions. * Sending lots of data is possible, but usually ends up being less efficient than classic * Bluetooth when trying to achieve maximum throughput. */
1.硬件超过20个字节分包发送 ,这个 都是比较合理的做法,客户端在组包
2.硬件不分包 ,一次发送超过20个字节。这个好像是不能超过512个字节 ,双方协议规定
二:来一张图片展示一下
三:解决方法
/** * 改变BLE默认的单次发包、收包的最大长度,用于android 5.0及以上版本 * @param mtu * @return */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static boolean requestMtu(int mtu){ //配置数据的大小 我配置的是512 if (mBluetoothGatt != null) { return mBluetoothGatt.requestMtu(mtu); } return false; }
这个方法在蓝牙连接成功后调用
总结:
经过测试是能接收到数据,小米手机7.0可以接收 ,其他手机我没有测试 。