【发布时间】:2021-11-29 03:58:03
【问题描述】:
我对 BLE 很陌生,我正在制作一个需要通过 BLE 快速连续传输 Data 对象的应用程序。到目前为止,我已经尝试了一个 for 循环来根据索引更改特征。它成功地改变了第一个值的特征,但在其余部分失败了。我目前正在开发一个 POC,其代码如下所示。
guard let characteristic = self.transferCharacteristic else { return }
for x in "Hello there" {
let data = Data("\(x)".data(using: .utf8)!)
let didSend = self.peripheralManager.updateValue(data, for: characteristic, onSubscribedCentrals: nil)
if didSend {
print("Sent: \(x)")
} else {
print("Couldn't send: \(x)")
}
}
这个的输出是,
Sent: H
Couldn't send: e
Couldn't send: l
Couldn't send: l
.
.
.
Couldn't send: e
Couldn't send: r
Couldn't send: e
我怎样才能实现我想要的? 有没有更好的办法?
我知道事务只有几毫秒,但不知道如何在可能的情况下将特征更改与事务速度同步。
提前谢谢你。
【问题讨论】:
-
我可能不太了解您的问题,但对我来说这听起来像是一个竞争条件。当您应该串联调用时,您实际上是在并行调用它们。在您获得第 4 项的结果之前,无法发送第 5 项。这是否正确?
-
我不确定 CoreBluetooth 是否在不同的线程上运行,但我可以向您保证,我不会并行更新特性。我在程序中只有必要的 CB 委托方法、一些变量(外设、外设管理器和特征)和一个 for 循环,用于更新“Hello there”中每个字母的一个特征
-
啊,对不起,我确实误解了你的问题。
标签: ios swift bluetooth-lowenergy core-bluetooth