简体   繁体   English

从BLE设备获取响应

[英]Getting response from BLE device

I'm quite new to working with bluetooth devices. 我对使用蓝牙设备很陌生。 So far everything works perfect. 到目前为止,一切正常。 But there's an issue I don't know how to deal with. 但是有一个我不知道如何处理的问题。 I have a led lamp device where I can change everything from color to speed, flashing, fading and so on. 我有一个led灯设备,可以在其中更改从颜色到速度,闪烁,褪色等的所有内容。 Now I want to read the current device state (is the device turned on or off for example). 现在,我想读取当前设备状态(例如,设备是打开还是关闭)。 I have a document for the device which says: 我有该设备的文件,上面写着:

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】 查询:a)发送顺序:【0XEF】+【0X01】+【0X77】b)控制器响应:【0X66】+【8bit设备名称(0x14】】+【8bit swtich开/关】+【8bit模式值】+ 【8位运行/暂停状态】+【8位速度值】+【8位红色数据】+【8位绿色数据】+【8位蓝色数据】+【8位预热期间】+【8位版本号】+【0X99】

How do I get the controller response? 如何获得控制器响应? The didWriteValueFor function just returns me wether or not the write call was successful. didWriteValueFor函数只是向我返回写入调用是否成功。

If characteristic supports notifying you can turn of notyfying with that line: 如果特性支持通知,您可以通过该行进行通知:

peripheral.setNotifyValue(true, for: characteristic)

It's very important to set delegate for peripheral with that characteristic to proper file, in my case it was: 将具有该特征的外围设备的委托设置为适当的文件非常重要,在我的情况下是:

peripheral.delegate = self

After that when something will change, peripheral will fire this method in your code: 之后,当某些事情发生变化时,外设将在您的代码中触发此方法:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    //data is in characteristic.value
}

If your characteristic does not support notyfying you can try to write data to peripheral with response type set to .withReponse like that: 如果您的特征不支持noyfying,则可以尝试将响应类型设置为.withReponse的数据写入外设,如下所示:

peripheral.writeValue(data, for: characteristic, type: .withResponse)

Remember to set peripheral delegate too, after that peripheral will fire this method after every successful write: 记住也要设置外围设备委托,在该外围设备每次成功写入后将触发此方法:

    //write response
    func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
        //data is in characteristic.value
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM