简体   繁体   English

iOS蓝牙BLE读取数据的最大大小

[英]iOS Bluetooth BLE read data maximum size

I have an iOS app that reads/writes on a BLE device. 我有一个可在BLE设备上读取/写入的iOS应用。 The device is sending me data over 20 bytes long and I see they get trimmed. 设备正在向我发送超过20个字节的数据,我看到它们被修整了。 Based on the following thread 基于以下线程

Bluetooth LE maximum transmission size 蓝牙LE最大传输大小

it looks like iOS is trimming the data. 好像iOS正在修剪数据。 That thread shows the solution on how to write bigger data sizes, but how do we read info larger than 20 bytes? 该线程显示了有关如何写入更大数据大小的解决方案,但是我们如何read大于20个字节的信息?

For anyone looking at this post years later like I am, we ran into this question as well at one point. 对于像我一样在几年后看这个帖子的任何人,我们也同时遇到了这个问题。 I would like to share some helpful hints for data larger than 20 bytes. 我想分享一些大于20个字节的数据的有用提示。

  • Since the data is larger than one packet can handle, you will need to send it in multiple packets. 由于数据大于一个数据包可以处理的大小,因此您将需要以多个数据包的形式发送数据。 It helps significantly if your data ALWAYS ends with some sort of END byte. 如果您的数据始终以某种END字节结尾,则将有很大帮助。 For us, our end byte gives the size of the total byte array so we can check that at the end of reading. 对于我们来说,我们的结束字节给出了总字节数组的大小,因此我们可以在读取结束时进行检查。
  • Create a loop that checks for a packet constantly and stops when it receives that end byte (would also be good to have a timeout for that loop). 创建一个循环,不断检查数据包,并在接收到该结束字节时停止该循环(对于该循环有一个超时也将是一件好事)。
  • Make sure to clear the "buffer" when you start a new read. 开始新的读取时,请确保清除“缓冲区”。
  • It is nice to have an "isBusy" boolean to keep track of whether another function is currently waiting to read from the device. 最好有一个“ isBusy”布尔值来跟踪当前是否有另一个函数正在等待从设备读取数据。 This prevents read overlaps. 这样可以防止读取重叠。 For us, if the port is currently busy, we wait a half second and try again. 对于我们来说,如果端口当前很忙,请等待半秒钟,然后重试。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM