且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

读取BLE外围设备特性并检查其值?

更新时间:2023-01-24 08:49:51

已为Swift3更新

Updated For 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,所以要放手一搏.

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小块和有意义的数字之间转换,请查看

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 {}