咱们运用nRF Connect
(一个调试蓝牙的第三方工具)先衔接一个设备,能够看到设备相关信息,如下:
图中标红色的便是特征值,代表相关服务的特性。特征值有三种,分别是readValue
(读取)、setNotifyValue
(订阅)、writeValue
(写入)。
readValue
阐明是可读的,回调方法为
// 值更新回调
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("特征值读取失败:\(error)")
return
}
if let data = characteristic.value {
print("特征值:\(data)")// 取具体类型的值时,需求依据相关的数据表结构做类型转换
}
}
一般来讲,同硬件工程师连调时会告知你该characteristic
对应的是readValue
(读取)、setNotifyValue
(订阅)、仍是writeValue
(写入),但假设硬件工程师不告知你呢,其实也很简单,发现characteristic
有个properties
特点,点击去检查,如下:
public struct CBCharacteristicProperties : OptionSet, @unchecked Sendable {
public init(rawValue: UInt)
public static var broadcast: CBCharacteristicProperties { get }
public static var read: CBCharacteristicProperties { get }
public static var writeWithoutResponse: CBCharacteristicProperties { get }
public static var write: CBCharacteristicProperties { get }
public static var notify: CBCharacteristicProperties { get }
public static var indicate: CBCharacteristicProperties { get }
public static var authenticatedSignedWrites: CBCharacteristicProperties { get }
public static var extendedProperties: CBCharacteristicProperties { get }
@available(watchOS 2.0, *)
public static var notifyEncryptionRequired: CBCharacteristicProperties { get }
@available(watchOS 2.0, *)
public static var indicateEncryptionRequired: CBCharacteristicProperties { get }
}
这样一来,咱们就能够用位运算来获取它是否能够read
,代码如下:
let isRead = (characteristic.properties.rawValue & CBCharacteristicProperties.writeWithoutResponse.rawValue) != 0 ? true : false
if isRead {
peripheral.readValue(ofCharac: characteristic) { result in
}
}
留意:假如尝试读取不可读的特征值,didUpdateValueFor回调将会回来错误error。
setNotifyValue
(订阅)阐明要对特征进行订阅,一旦外设有值更新时,咱们就能够收到这个新值,便是KVO
。相同,咱们也需求知道该characteristic
支不支持订阅,代码如下:
let enble = (characteristic.properties.rawValue & CBCharacteristicProperties.notify.rawValue) != 0 ? true : false
if enble == true {
peripheral.setNotifyValue(true, for: characteristic) // 开启订阅
}
订阅成功后,只要外设值发生变化时,咱们就能够收到回调:
// 特征值订阅状况回调
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("特征值订阅失败:\(error)")
return
}
}
writeValue
是向外设写入指令,比如跑步机的启停、调速等,来完成一些操作使命。
/*
data:写入的值
characteristic:待写入的特征对象
type:写入类型,包括以下类型
withResponse:向外设传递特征值,并回来写入状况
withoutResponse:向外设传递特征值,不会回来写入状况
*/
open func writeValue(_ data: Data, for characteristic: CBCharacteristic, type: CBCharacteristicWriteType)
同理,也是依据properties 特点来判别是withResponse
仍是withoutResponse
,若该characteristic
为withoutResponse
,可是传入的是withResponse
会导致犯错。
let type = (characteristic.properties.rawValue & CBCharacteristicProperties.writeWithoutResponse.rawValue) != 0 ? CBCharacteristicWriteType.withoutResponse : CBCharacteristicWriteType.withResponse
peripheral.writeValue(ofCharac: characteristic, value: data,type:type) { result in
}
这便是readValue
(读取)、setNotifyValue
(订阅)、writeValue
(写入)的含义和运用,有对常用API不清楚的同学能够看看蓝牙常用API。