How to get data from a Bluetooth feature in Swift

I now have a Polar h7 device around me (it's BTLE) and everything works for me, but I'm confused about how to get BPM from characteristic.value now that it is being updated. I have to turn some bytes into bpm ...

My peripheral updates:

 func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) { if characteristic.UUID == CBUUID.UUIDWithString(heartRateChar) { getInfoAboutHeartRate(characteristic) } } 

I get heart rate information:

 func getInfoAboutHeartRate(characteristic:CBCharacteristic) { println(characteristic.value) var bytes = characteristic.value.bytes } 

I know that I need to convert these bytes to BPM.
According to the specifications (they confuse me here) in bluetooth.org , byte 0 will either be 1 or 0 .. If this value is 0 , the heart rate value is uint8 , and if it is 1 , then it is uint16 , and I can get BPM from this.
How do I know if byte 0 is 1 or 0 ? How to turn it into uint8 or uint16 . And if I do this, will I get BPM directly or do I need to do something else for this? BPM is now returning as something like <16447d03> , which makes sense.

+6
source share
2 answers

It seems like it should be simple enough, but try to confirm what you are saying. Does "bytes" mean "16447d03"? This is 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" are an array containing the actual values.

From the private discussion that we had, you indicated the result 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 were 22 in all cases that you specified. This makes sense, like everything else, from the same heart rate equipment.

Bits are grouped as follows: | 3 bits reserved | 1 bit for RR interval | 1 bit for power consumption | 2 bits for sensor contact status | 1 bit for heart rate format | 22 - 00010110 in binary format, which is equal to | 000 | 1 | 0 | 11 | 0 |.

Bit rate format: 0 (Heart rate value format is set to UINT8)
Sensor contact status values: 3 (sensor contact function is supported and a contact is detected)
Energy Consumption of status bits: 0 (Energy Field not considered)
RR-Interval bit: 1 (one or more RR-Interval values ​​present)

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

Thus, for these data, the heart rate was 77, 78, 79, 78.

+12
source

If anyone is interested how to calculate the RR value. Take the first array as an example:

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

If we break this 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 that the LSO order (least significant octet) for MSO (most significant octet) 22 and 3 needs to be swapped leaving:

 790 or 0000 0011 0001 0110 

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

+1
source

Source: https://habr.com/ru/post/974236/


All Articles