我对使用蓝牙设备还很陌生。到目前为止一切都很完美。但有一个问题我不知道如何处理。 我有一个 LED 灯设备,我可以改变一切,从颜色到速度、闪烁、褪色等等。 现在我想读取当前的设备状态(例如设备是打开还是关闭)。我有一份该设备的文档,内容如下:
Query: a) Send order:【0XEF】+【0X01】+【0X77】 b) Controller response: 【0X66】+【8bit device name(0x14)】+【8bit swtich on /off】+【8bit mode value】+【8bit run/pause state】+【8bit speed value】+【8bit red data】+【8bit green data】+【8bit blue data】+【8bit warm while】+【8bit version number】+【0X99】
如何获得 Controller 响应? didWriteValueFor 函数只返回我写入调用是否成功。
最佳答案
如果特性支持通知,您可以使用该行关闭通知:
peripheral.setNotifyValue(true, for: characteristic)
将具有该特征的外围设备的委托(delegate)设置为正确的文件非常重要,在我的例子中是:
peripheral.delegate = self
之后,当某些内容发生变化时,外围设备将在您的代码中触发此方法:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
//data is in characteristic.value
}
如果您的特性不支持通知,您可以尝试将数据写入外设,并将响应类型设置为.withReponse,如下所示:
peripheral.writeValue(data, for: characteristic, type: .withResponse)
记住也要设置外设委托(delegate),之后外设将在每次成功写入后触发此方法:
//write response
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
//data is in characteristic.value
}
关于ios - 获取 BLE 设备的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41035849/