gpt4 book ai didi

ios - 从 Swift 写入 BLE 外设

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:02 26 4
gpt4 key购买 nike

我正在构建一个连接到 BLE device (Redbear Labs Duo) 的 iOS Swift 应用程序.

什么有效?

  1. 我能够启动设备扫描
  2. 连接到设备
  3. 收集 BLE 设备的服务和特征 - 一切正常

问题出在哪里?

  1. BLE 设备 (Redbear Labs Duo) 也有一个板载 Wifi Controller ,能够扫描可用网络。 The documentation声明要扫描 Wifi 必须
    • 使用 UUID 3EC61400-89CD-49C3-A0D9-7A85669E901E 连接到主要服务
    • 找到UUID为3EC61401-89CD-49C3-A0D9-7A85669E901E的命令特征
    • 发送一个2字节的命令[0x20, 0xA0]到命令特征
    • 还设置一个 1 字节状态指示符 0xB1 到具有 UUID 3EC61402-89CD-49C3-A0D9-7A85669E901E 的扫描特征

我执行上述步骤的代码如下..

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?)
}
}

更新:

我设置了一堆调试语句并得到了以下输出 - 从下面可以明显看出

  1. 我能够识别并连接到正确的设备和特征
  2. 我能够适本地设置通知值

[DEBUG] Connecting to peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Connected to peripheral 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Did connect to peripheral
[DEBUG] Found service: 3EC61400-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Found characteristic: 3EC61401-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
[DEBUG] Found characteristic: 3EC61402-89CD-49C3-A0D9-7A85669E901E for peripheral: 547BC3C9-4823-431C-B888-A8F3E8C699F5
("3EC61402-89CD-49C3-A0D9-7A85669E901E", )
("3EC61401-89CD-49C3-A0D9-7A85669E901E", )
[DEBUG] didUpdateNotification state for characteristic CBCharacteristic: 0x1702a6c60, UUID = 3EC61401-89CD-49C3-A0D9-7A85669E901E, properties = 0x14, value = (null), notifying = YES on peripheral: CBPeripheral: 0x1740fba80, identifier = 547BC3C9-4823-431C-B888-A8F3E8C699F5, name = Duo-ZKBY, state = connected
[DEBUG] didUpdateNotification state for characteristic: CBCharacteristic: 0x1742a3c60, UUID = 3EC61402-89CD-49C3-A0D9-7A85669E901E, properties = 0x10, value = (null), notifying = YES on peripheral: CBPeripheral: 0x1740fba80, identifier = 547BC3C9-4823-431C-B888-A8F3E8C699F5, name = Duo-ZKBY, state = connected

最佳答案

找到解决方案 - 在此发布以造福他人。

阅读Redbear Labs 提供的文档,需要注意的关键是Command 特性仅支持两种类型的属性- PROPERTY_WRITE_NO_RESPONSE | PROPERTY_NOTIFY

同样,Scan 特性仅支持 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 扫描开始时,Scan 特性将发送一个值为 0xB1 的通知以指示扫描开始,然后发送一个值为 0xB2 的通知以指示扫描结束。

扫描到的实际 Wifi 网络将通过命令特征本身的通知发送。

关于ios - 从 Swift 写入 BLE 外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41029477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com