读取 BLE 外设特性并检查其值? [英] Reading a BLE Peripheral Characteristic and checking its value?
问题描述
我正在 Xcode 上使用 Swift 编写一个应用程序,该应用程序连接到蓝牙 BLE 外设.我已经建立了与设备的连接,并想从特定特征(特别是服务 UUID FFF0 中的 FFF1)读取一些数据.
I'm writing an app using Swift on Xcode that connects to a bluetooth BLE peripheral. I've established a connection to a the device, and want to read some data from a specific characteristic (specifically FFF1 in service UUID FFF0).
如果我要查找信息的特征是 characteristicx
,我可以使用以下代码请求读取特征:
I'm able to request a read of characteristics using the following code if the characteristic that I want to find info for is characteristicx
:
peripheral.readValueForCharacteristic(charactericsx)
我想知道的是:如何检查此读取值是否是我要查找的值.我希望能够执行 if 语句来根据该特征的发现值检查我的值.
What I want to know is this: How do I check that this read value is what I'm looking for. I want to be able to do an if statement to check my value against the discovered value for that characteristic.
例如:如果发现的值为 X 则做某事,否则如果发现的值为 Y 则做其他的事情.
Eg: If discovered value is X then do something, else if discovered value is Y then do something else.
这不是我想要做什么的一个很好的解释,但我希望你明白要点.
That's not a very good explanation of what I want to do, but I hope you get the gist.
有人知道怎么做吗?
推荐答案
已针对 Swift3 更新
执行该方法后,外围设备的delegate
将异步接收peripheral(_:didUpdateValueFor:error:)
方法.在该方法中,您可以查询传递的 characteristic
参数的 value
.value
将是一个 NSData
,您可以从中提取字节.例如
After you execute that method, the delegate
of your peripheral is going to asynchronously receive the peripheral(_:didUpdateValueFor:error:)
method. In that method you can query the value
of the passed characteristic
parameter. value
will be an NSData
which you can pull the bytes out of. E.g.
// MARK: - CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let e = error {
print("ERROR didUpdateValue (e)")
return
}
guard let data = characteristic.value else { return }
...
}
value
方法实际上在预期的 Data
周围返回了一个 Optional
,所以要使用 let 守卫.
The value
method actually returns an Optional
around the expected Data
, so a let guard is the way to go.
通常,一个特性会有一个简单的值编码在它的最多 20 字节的 Data
负载中.例如.也许这是一个简单的 UInt16 计数器.到
Usually a characteristic will have a simple value encoded in it's up-to-20-byte Data
payload. E.g. maybe it's a simple UInt16 counter. To
要在这些 Data
数据和有意义的数字之间进行转换,请查看 往返 Swift 数字类型到/从数据(我在下面包含了我自己的实现).
To convert between these Data
glumps and meaningful numbers, have a look at the answer to round trip Swift number types to/from Data (I've included my own implementation of that below).
因此,例如,如果您知道感兴趣的特征是某个旨在作为 UInt16
提取的计数器,我将使用以下内容填写上面的示例:
So for example, if you know that the characteristic of interest is some counter that is a meant to be extracted as a UInt16
, I would fill out the above example with something like:
// MARK: - CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let e = error {
print("ERROR didUpdateValue (e)")
return
}
guard let data = characteristic.value else { return }
print("counter is (UInt16(data:data))")
}
// Data Extensions:
protocol DataConvertible {
init(data:Data)
var data:Data { get }
}
extension DataConvertible {
init(data:Data) {
guard data.count == MemoryLayout<Self>.size else {
fatalError("data size ((data.count)) != type size ((MemoryLayout<Self>.size))")
}
self = data.withUnsafeBytes { $0.pointee }
}
var data:Data {
var value = self
return Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
}
}
extension UInt8:DataConvertible {}
extension UInt16:DataConvertible {}
extension UInt32:DataConvertible {}
extension Int32:DataConvertible {}
extension Int64:DataConvertible {}
extension Double:DataConvertible {}
extension Float:DataConvertible {}
这篇关于读取 BLE 外设特性并检查其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!