如何解析蓝牙外设特性值

时间:2016-09-18 01:46:32

标签: ios swift core-bluetooth

我以前没有使用蓝牙传感器和解析原始值的经验。我试图解析特征值,但我不确定如何实际阅读和解析它。我试图解析的特征的数据描述在这里:https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.csc_measurement.xml

以下是代码

   func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
        parseData(characteristic.value)
    }

    private func parseData(data: NSData?) {
        if let data = data {
            var values = [UInt8](count:data.length, repeatedValue:0)
            data.getBytes(&values, length:data.length)

            print(values)
        }
    }

输出看起来像这样

[2, 14, 0, 16, 56]
[2, 16, 0, 249, 62]
[2, 18, 0, 175, 191]
[2, 23, 0, 90, 195]
[1, 2, 0, 0, 0, 251, 206]
[1, 3, 0, 0, 0, 145, 208]
[1, 4, 0, 0, 0, 5, 212]
[1, 5, 0, 0, 0, 25, 217]

现在首先,我不确定我是否必须将其解析为UInt8,这是不清楚的部分,我无法理解蓝牙网站上说有什么是UInt32和UInt16变量。我如何从单个原始数据中解析它。我想我会把这整件事弄错,但那就是我想要了解的内容。提前致谢。

0 个答案:

没有答案