从 BLE 设备读取值
Reading values from BLE device
我拥有一条 Polar H10 胸带,它以低功耗蓝牙运行并提供心率和心率变异性。
我想用 Android 应用程序读出这些值。感谢 official BLE tutorial 中的帮助,我能够连接到设备。现在的问题是从设备中读出心率和心率变异性值。每当设备上有一个新值时,我都想读出这个值(并且至少每秒都有一个新值)。
我找到了以下代码片段:
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;
的描述
This depends on hear rate value format and if there is energy data
我不明白这个offest的意思。我应该使用什么偏移值?
其次,如果连接中断,我希望收到通知以便我可以处理它(例如显示消息)。我该怎么做?
关于这件事我可以帮你做几件事。
对于连接中断,你可以这样处理
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
}
您还可以在连接到 Gatt
时将“自动连接”选项设置为 'True'
bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback);
对于创建服务,您可以创建一个 Sticky 服务,它会自行重启,除非强制停止。
我拥有一条 Polar H10 胸带,它以低功耗蓝牙运行并提供心率和心率变异性。
我想用 Android 应用程序读出这些值。感谢 official BLE tutorial 中的帮助,我能够连接到设备。现在的问题是从设备中读出心率和心率变异性值。每当设备上有一个新值时,我都想读出这个值(并且至少每秒都有一个新值)。
我找到了以下代码片段:
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;
的描述
This depends on hear rate value format and if there is energy data
我不明白这个offest的意思。我应该使用什么偏移值?
其次,如果连接中断,我希望收到通知以便我可以处理它(例如显示消息)。我该怎么做?
关于这件事我可以帮你做几件事。 对于连接中断,你可以这样处理
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
}
您还可以在连接到 Gatt
时将“自动连接”选项设置为 'True'bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback);
对于创建服务,您可以创建一个 Sticky 服务,它会自行重启,除非强制停止。