-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeartRateMeasurementParser.java
144 lines (127 loc) · 5.67 KB
/
HeartRateMeasurementParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2015, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package no.nordicsemi.android.nrftoolbox.parser;
import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Environment;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.System;
public class HeartRateMeasurementParser {
private static final byte HEART_RATE_VALUE_FORMAT = 0x01; // 1 bit
private static final byte SENSOR_CONTACT_STATUS = 0x06; // 2 bits
private static final byte ENERGY_EXPANDED_STATUS = 0x08; // 1 bit
private static final byte RR_INTERVAL = 0x10; // 1 bit
public static String parse(final BluetoothGattCharacteristic characteristic) {
int offset = 0;
final int flags = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset++);
/*
* false Heart Rate Value Format is set to UINT8. Units: beats per minute (bpm)
* true Heart Rate Value Format is set to UINT16. Units: beats per minute (bpm)
*/
final boolean value16bit = (flags & HEART_RATE_VALUE_FORMAT) > 0;
/*
* 0 Sensor Contact feature is not supported in the current connection
* 1 Sensor Contact feature is not supported in the current connection
* 2 Sensor Contact feature is supported, but contact is not detected
* 3 Sensor Contact feature is supported and contact is detected
*/
final int sensorContactStatus = (flags & SENSOR_CONTACT_STATUS) >> 1;
/*
* false Energy Expended field is not present
* true Energy Expended field is present. Units: kilo Joules
*/
final boolean energyExpandedStatus = (flags & ENERGY_EXPANDED_STATUS) > 0;
/*
* false RR-Interval values are not present.
* true One or more RR-Interval values are present. Units: 1/1024 seconds
*/
final boolean rrIntervalStatus = (flags & RR_INTERVAL) > 0;
// heart rate value is 8 or 16 bit long
int heartRateValue = characteristic.getIntValue(value16bit ? BluetoothGattCharacteristic.FORMAT_UINT16 : BluetoothGattCharacteristic.FORMAT_UINT8, offset++); // bits per minute
if (value16bit)
offset++;
// energy expanded value is present if a flag was set
int energyExpanded = -1;
if (energyExpandedStatus)
energyExpanded = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset);
offset += 2;
// RR-interval is set when a flag is set
final List<Float> rrIntervals = new ArrayList<>();
if (rrIntervalStatus) {
for (int o = offset; o < characteristic.getValue().length; o += 2) {
final int units = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, o);
rrIntervals.add(units * 1000.0f / 1024.0f); // RR interval is in [1/1024s]
}
}
final StringBuilder builder = new StringBuilder();
builder.append("Heart Rate Measurement: ").append(heartRateValue).append(" bpm");
switch (sensorContactStatus) {
case 0:
case 1:
builder.append(",\nSensor Contact Not Supported");
break;
case 2:
builder.append(",\nContact is NOT Detected");
break;
case 3:
builder.append(",\nContact is Detected");
break;
}
if (energyExpandedStatus)
builder.append(",\nEnergy Expanded: ").append(energyExpanded).append(" kJ");
if (rrIntervalStatus) {
builder.append(",\nRR Interval: ");
for (final Float interval : rrIntervals)
builder.append(String.format(Locale.US, "%.02f ms, ", interval));
builder.setLength(builder.length() - 2); // remove the ", " at the end
}
/*
File root = new File(Environment.getExternalStorageDirectory(), "Nordic Semiconductor");
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
if (!root.exists()) {
root.mkdir();
}
StringBuilder builder2 = builder;
builder2.append("time: ").append(System.currentTimeMillis());
try {
//File dest = File.createTempFile("data", "csv", root);
File dest = new File(baseDir + File.separator + fileName);
FileOutputStream outputStream = new FileOutputStream(dest);
try {
outputStream.write(builder2.toString().getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
*/
return builder.toString();
}
}