私はblueooth低エネルギーで動作し、心拍数と心拍数変動性を提供するPolar H10胸部ストラップを所有しています。BLE装置から値を読み取る
これらの値をAndroidアプリで読みたいと思います。 official BLE tutorialのヘルプのおかげで、デバイスに接続できます。問題は、デバイスから心拍数および心拍変動の値を読み取ることです。新しい値がデバイス上で利用可能になるたびに、私はこの値を読みたい(少なくとも1秒おきに新しい値がある)。
私は次のコードの断片を発見した:
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()
方法で
は説明
これが聞くレート値のフォーマットに依存したエネルギーデータがある場合
私はこのoffestの意味を理解していないとint offset = 1;
あり。どんなオフセット値を使うべきですか?
第2に、接続が切断された場合、処理できるように通知を受けたい(メッセージの表示など)。どうやってやるの?