从Swift

时间:2016-12-07 23:53:38

标签: ios swift bluetooth-lowenergy

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

什么有用?

  1. 我可以启动设备扫描
  2. 连接到设备
  3. 收集BLE设备的服务和特性 - 一切都很好
  4. 问题出在哪里?

    1. BLE设备(Redbear Labs Duo)还具有板载Wifi控制器,能够扫描可用网络。 The documentation声明必须扫描Wifi
      • 使用UUID连接到主服务3EC61400-89CD-49C3-A0D9-7A85669E901E
      • 使用UUID 3EC61401-89CD-49C3-A0D9-7A85669E901E找到命令特征
      • 将2字节命令[0x20,0xA0]发送到命令特征
      • 还使用UUID 3EC61402-89CD-49C3-A0D9-7A85669E901E
      • 将1字节状态指示符0xB1设置为扫描特性
    2. 我执行上述步骤的代码如下:

      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. 我能够正确设置通知值
      3.   

        [DEBUG]连接到外围设备:547BC3C9-4823-431C-B888-A8F3E8C699F5
          [DEBUG]连接到外围设备547BC3C9-4823-431C-B888-A8F3E8C699F5
          [DEBUG]连接到外围设备了   [DEBUG]发现服务:3EC61400-89CD-49C3-A0D9-7A85669E901E用于外围设备:547BC3C9-4823-431C-B888-A8F3E8C699F5
          [DEBUG]发现特性:3EC61401-89CD-49C3-A0D9-7A85669E901E用于外围设备:547BC3C9-4823-431C-B888-A8F3E8C699F5
          [DEBUG]发现特性:3EC61402-89CD-49C3-A0D9-7A85669E901E用于外围设备:547BC3C9-4823-431C-B888-A8F3E8C699F5
          (" 3EC61402-89CD-49C3-A0D9-7A85669E901E",)
          (" 3EC61401-89CD-49C3-A0D9-7A85669E901E",)
          [DEBUG] didUpdateNotification状态为特征CBC特征:0x1702a6c60,UUID = 3EC61401-89CD-49C3-A0D9-7A85669E901E,属性= 0x14,值=(null),通知= YES外设: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

2 个答案:

答案 0 :(得分:4)

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

阅读Redbear Labs提供的文档,需要注意的关键是Command特性仅支持两种类型的属性 - 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网络将通过命令特征本身的通知发送。

答案 1 :(得分:0)

致电

>>> dt=datetime.datetime(2019, 12, 24, 0, 0)
>>> dt.timetuple()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=358, tm_isdst=-1)

您需要在此委托函数中调用read:

peripheral.writeValue(dataToWrite, for: char, type: .withResponse)
相关问题