iOS蓝牙LE-正确的无响应书写方式 [英] iOS Bluetooth LE - Proper Way to Write Without Response

查看:253
本文介绍了iOS蓝牙LE-正确的无响应书写方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iOS CoreBluetooth,在发送相对大量的数据时,将其分解为20个字节的块,然后一次将其写入外围对象很重要.当使用WriteWithResponse特性时,这很容易做到:写20个字节,等待回调,写下20个字节,依此类推.

With iOS CoreBluetooth, when sending a relatively large amount data, it's important to break it up into 20 byte chunks and then write them one at a time into the peripheral object. This is pretty easy to do when using a WriteWithResponse characteristic: write 20 bytes, wait for the callback, write the next 20 bytes, and so on.

但是具有WriteWithoutResponse特性的那又如何呢?我需要通过BLE尽快发送1-2kB的数据. WriteWithResponse在执行此操作时效率很低,因为它会每隔20个字节发送一次数据包.错误校正和可靠性在我的应用层得到了照顾,因此我不需要BLE来确认数据.

But what about with a WriteWithoutResponse characteristic? I need to send of 1-2kB of data as quickly as I can over BLE. WriteWithResponse is very inefficient at doing this, because it acks every 20 byte packet. Error correction and reliability are taken care of at my application layer, so I have no need for BLE to ack the data.

问题在于WriteWithoutResponse不会给您回调,因为CoreBluetooth无法知道何时实际写入数据.所以问题是:如何使用WriteWithoutResponse适当地间隔发送大量数据?

The issue is that WriteWithoutResponse does not give you a callback, because there is no way for CoreBluetooth to know when the data was actually written. So the question is: how do we properly space out sending a large amount of data using WriteWithoutResponse?

我想到的唯一解决方案是执行以下操作:

The only solution I've thought of is to do the following:

  1. 获取连接间隔和每个连接间隔链接能够容纳的数据包数量.
  2. 立即写入每个20字节的X数据包,等待Y时间,然后重复进行直到没有剩余数据为止. (X =每个连接间隔的数据包数量,Y =连接间隔)

此方法存在两个明显的问题:

There are two glaring problems with this approach:

  1. CoreBluetooth不会向我们公开连接间隔(为什么?).因此,有两种选择.第一个是:猜猜.根据您的首选连接参数,可能是更坏情况还是平均情况,我认为iOS喜欢选择30ms.但这是一个坏主意,因为中心有权完全忽略建议的参数.第二个是您可以拥有外围设备商店,并将商定的CI传输到iOS设备.问题是在iOS设备完成发现服务和特征并订阅适当的通知之前,您无法发送CI.因此,在发送CI之前,您必须在连接后设置一些固定的延迟,或者从iOS设备发送少量数据以通知外围设备已准备就绪.两者都会产生延迟,并且是非常差的解决方案.
  2. 我们不知道每个连接间隔可以支持多少个数据包.理论上最大为6.但是平均情况可能为4或更小.它还取决于外围设备.

当然,发送大量数据的一个不错的选择是将MTU的大小增加到大于20个字节,以容纳我们的大量数据.但是似乎很少有外围设备支持这一点.我们没有.

Of course a great option for sending large amounts of data is to increase the MTU size to larger than 20 bytes to accommodate our large amount of data. But it seems few peripherals support this; ours does not.

有人对如何解决这个问题有见解吗?

Anyone have any insights on how to solve this?

推荐答案

如果您支持iOS 11: iOS网站

If You are supporting iOS 11: iOS Website

@property(readonly) BOOL canSendWriteWithoutResponse;

此属性使您知道Buffer是否已满,并且可以传输更多而无响应. 每次传输后,请留意此变量并进行回调:外围设备代表

This property lets you know, if Buffer is filled up or not and can transmit more without response. After each transmission keep an eye on this variable and a call back: Peripheral Delegate

peripheralIsReadyToSendWriteWithoutResponse:

什么时候足以让您知道何时发送更多数据.

Which should be sufficient to let you know, when to send more data.

这篇关于iOS蓝牙LE-正确的无响应书写方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆