How to get data out of bluetooth characteristic in Swift

为君一笑 提交于 2019-12-03 00:30:19

This seems like it should be simple enough, but trying to confirm what you're saying. Does “bytes” end up with a value of “16447d03”? It's a pointer to the data, not the data itself, so you need to do something like this to get the actual value:

var data = characteristic.value
var values = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&values, length:data.length)

In this, “values” is an array containing the actual values.

From a private discussion we had, you listed the output as:

[22, 77, 22, 3] 
[22, 78, 27, 3, 18, 3] 
[22, 79, 2, 3] 
[22, 78, 15, 3] 

The first byte is the flags, which has been 22 in all the cases you've listed. This makes sense as it's all from the same heart rate hardware.

The bits are grouped like this: | 3 bits are reserved | 1 bit for RR-Interval | 1 bit for Energy Expended Status | 2 bits for Sensor Contact Status | 1 bit for Heart Rate Value Format | 22 is 00010110 in binary, which is | 000 | 1 | 0 | 11 | 0 |.

Heart Rate Value Format bit: 0 (Heart Rate Value Format is set to UINT8)
Sensor Contact Status bits: 3 (Sensor Contact feature is supported and contact is detected)
Energy Expended Status bit: 0 (Energy Expended field is not present)
RR-Interval bit: 1 (One or more RR-Interval values are present)

This means that the following byte is the heart rate (C1 field) and the remaining bytes are RR-Interval values, whatever they are (C4 field).

So for this data the heart rates were 77, 78, 79, 78.

If anyone's wondering how to calculate the RR value. Taking the first array as an example:

[22, 77, 22, 3] in binary is 0001 0110  0100 1101  0001 0110 0000 0011

If we break it a part we have:

Flag (8bit) = 22 or 0001 0110
HRV (8bit) = 77 or 0100 1101
RR (16bit) = 22 & 3 or 0001 0110 0000 0011

Because Bluetooth.org says the order is LSO (Least Significant Octet) to MSO (Most significant Octet) 22 and 3 need to be swapped leaving:

790 or 0000 0011 0001 0110

Because the resolution is 1/1024 seconds. The RR value = 790/1024 = 0.77s.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!