在 IOS 应用程序中获取并显示外设蓝牙 RSSI 信号强度

get and display peripheral bluetooth RSSI signal strength in IOS app

我在尝试从外围设备获取蓝牙 RSSI 信号强度的 IOS swift 应用程序中遇到问题。我一直在尝试使用 readRSSI()(参见下面的代码),其中 returns 是一个 Future,但到目前为止我无法将该 Future 映射到另一个可用变量,例如 Int 或 String。我是 Swift 的新手,所以不确定我是否缺少异步步骤或其他步骤。我习惯于在 R、python、JS 中工作,并且在语法上遇到了一些挑战。任何帮助是极大的赞赏。

我试过将多种从扩展中提取内容的方法切换到 ViewController,但没有成功。无论我如何尝试传递 Future 类型值,我都会收到类型不匹配的错误。

let strengthCharacteristic = self.peripheral.readRSSI()
let thisRet = self.strengthChar.map({ avar in 
    return avar
})
self.strengthLabel.text = String(thisRet ?? 0)

Apple docs 用于 readRSSI() 方法:

On iOS and tvOS, when you call this method to retrieve the RSSI of the peripheral while connected to the central manager, the peripheral calls the peripheral:didReadRSSI:error: method of its delegate object, which includes the RSSI value as a parameter.

为了从外围设备读取 RSSI,在外围设备的委托对象中实现 peripheral:didReadRSSI:error:。当您调用外围对象的 readRSSI() 方法时,将触发此方法。您可以在此方法中直接检索 RSSI 值作为输入参数。不要忘记将委托分配给外围对象。