我正在构建一个iOS Swift应用程序,连接到一个贝尔设备(红熊实验室二人)。
什么管用?
问题出在哪里?
我执行上述步骤的代码如下..
func scanWifi() {
print("[DEBUG] - Scanning for Wifi")
let command:[UInt8] = [0x02, 0xA0]
let commandData = NSData(bytes: command, length: command.count)
BLE.sharedInstance.write(toCharacteristic: BLE.sharedInstance.RBL_CHAR_CMD_UUID, data: commandData, withType: .withResponse)
let state:[UInt8] = [0xB1]
let stateData = NSData(bytes: state, length: state.count)
BLE.sharedInstance.write(toCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID, data: stateData, withType: .withResponse)
BLE.sharedInstance.read(fromCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID)
}一切正常..。但是..。在将上述数据写入外围设备后,我期望下面的方法会被调用--它从未调用过。我做错什么了?
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if error != nil {
print("[ERROR] Error updating value. \(error!.localizedDescription)")
return
}
if characteristic.uuid.uuidString == RBL_CHAR_CMD_UUID {
self.delegate?.bleDidReceiveData(data: characteristic.value as NSData?)
}
}更新:
我设置了一组调试语句,并获得了以下输出--从下面可以看出
调试连接到外围设备:547 BC3C9-4823-431C-B 888-A8F3E8C699F5 调试连接到外围设备547 BC3C9-4823-431C-B 888-A8F3E8C699F5 调试确实连接到外围设备。 调试发现服务: 3EC61400-89CD-49C3-A0D9-7A85669E901E外围设备:547BC3C9-4823-431C-B 888-A8F3E8C699F5 调试功能: 3EC61401-89CD-49C3-A0D9-7A85669E901E外设:547BC3C9-4823-431C-B 888-A8F3E8C699F5 调试功能: 3EC61402-89CD-49C3-A0D9-7A85669E901E外设:547BC3C9-4823-431C-B 888-A8F3E8C699F5 ("3EC61402-89CD-49C3-A0D9-7A85669E901E“) ("3EC61401-89CD-49C3-A0D9-7A85669E901E“) 为下列特性调试didUpdateNotification状态: 0x1702a6c60,UUID = 3EC61401-89CD-49C3-A0D9-7A85669E901E,属性= 0x14,值= (null),外围通知= YES : CBPeripheral: 0x1740fba80,标识符=547BC3C9-4823-431C-B 888-A8F3E8C699F5,name =Duo,state = connected 为特征调试didUpdateNotification状态: CBCharacteristic: 0x1742a3c60,UUID = 3EC61402-89CD-49C3-A0D9-7A85669E901E,属性= 0x10,值= (null),外围通知= YES : CBPeripheral: 0x1740fba80,标识符= 547BC3C9-4823-431C-B888-A8F3E8C699F5,name =Duo,state = connected
发布于 2016-12-08 13:55:11
找到了解决方案--为了他人的利益而在这里张贴。
阅读红熊实验室提供的文档,需要注意的关键是命令特性只支持两种类型的属性-- PROPERTY_WRITE_NO_RESPONSE | PROPERTY_NOTIFY
同样,扫描特性只支持PROPERTY_NOTIFY
另外,要让设备扫描所有可用的Wifi,我们只应该将一个2字节的命令[0x20, 0xA0]写到命令特征代码下面
func scanWifi() {
print("[DEBUG] - Scanning for Wifi")
let command:[UInt8] = [0x02, 0xA0]
let commandData = NSData(bytes: command, length: command.count)
BLE.sharedInstance.writeTo(characteristic: BLE.sharedInstance.RBL_CHAR_CMD_UUID, data: commandData, withType: .withoutResponse)
}不需要写到扫描特征。当Wifi扫描开始时,扫描特性将发送一个带有值0xB1的通知来指示扫描的开始,然后发送一个带有值0xB2的通知来指示扫描的结束。
扫描的实际Wifi网络将通过命令特性本身的通知发送。
发布于 2019-12-24 03:44:38
打电话后
peripheral.writeValue(dataToWrite, for: char, type: .withResponse)您需要在这个委托函数中调用read:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
// Call your read functions here.
BLE.sharedInstance.read(fromCharacteristic: BLE.sharedInstance.RBL_CHAR_SCN_UUID)
}https://stackoverflow.com/questions/41029477
复制相似问题