我拥有一个极地H10胸带,它的运作与蓝宝石低能量,并提供心率和心率变异性。
我想用Android应用程序读出这些值。由于官方BLE教程中的帮助,我能够连接到设备。现在的问题是从设备中读出心率和心率变异性值。每当设备上有一个新值可用时,我都要读出这个值(至少每秒钟有一个新值)。
我发现了以下代码:
private static double extractHeartRate(
BluetoothGattCharacteristic characteristic) {
int flag = characteristic.getProperties();
Log.d(TAG, "Heart rate flag: " + flag);
int format = -1;
// Heart rate bit number format
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
}
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
return heartRate;
}
private static Integer[] extractBeatToBeatInterval(
BluetoothGattCharacteristic characteristic) {
int flag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
int format = -1;
int energy = -1;
int offset = 1; // This depends on hear rate value format and if there is energy data
int rr_count = 0;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
offset = 3;
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
offset = 2;
}
if ((flag & 0x08) != 0) {
// calories present
energy = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset);
offset += 2;
Log.d(TAG, "Received energy: {}"+ energy);
}
if ((flag & 0x16) != 0){
// RR stuff.
Log.d(TAG, "RR stuff found at offset: "+ offset);
Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
rr_count = ((characteristic.getValue()).length - offset) / 2;
Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
Log.d(TAG, "rr_count: "+ rr_count);
if (rr_count > 0) {
Integer[] mRr_values = new Integer[rr_count];
for (int i = 0; i < rr_count; i++) {
mRr_values[i] = characteristic.getIntValue(
BluetoothGattCharacteristic.FORMAT_UINT16, offset);
offset += 2;
Log.d(TAG, "Received RR: " + mRr_values[i]);
}
return mRr_values;
}
}
Log.d(TAG, "No RR data on this update: ");
return null;
}
假设我与设备有连接,如何使用它提取心率和r-r间期(节拍到节拍间隔)?如果有人能做个简短的例子,我会很高兴的。此外,我想使用一个服务,以便它在后台运行,我可以做其他工作,它消耗更少的电池。但是这个服务应该有最高优先级,这样它就不会被杀死。
在extractBeatToBeatInterval()
方法中,有一个带有描述的int offset = 1;
这取决于听觉速率值的格式和是否有能量数据。
我不明白这件事的意义。我应该使用什么偏移值?
其次,如果连接中断,我希望得到一个通知,以便我能够处理它(例如显示一条消息)。我怎么能这么做?
发布于 2017-11-15 09:28:03
在这件事上我可以帮你几件事。对于连接中断,您可以这样处理
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.e(Constants.TAGGattConnectThread, "Connected to the GATT Server " + status + " " +newState);
Log.e(Constants.TAGGattConnectThread, "Attempting to discover services");
bluetoothGatt.discoverServices();
}
else if(newState == BluetoothGatt.STATE_DISCONNECTED)
{
Log.e(Constants.TAGGattConnectThread,"Band disconnected");
//Write you disconnect handling code here
}
您还可以在连接到关贸总协定时将“AutoConnect”选项设置为“True”。
bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback);
对于创建服务,您可以创建一个Sticky服务,除非强制停止,否则该服务将重新启动自身。
https://stackoverflow.com/questions/44376839
复制相似问题