2017-06-05 12 views
0

私は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に、接続が切断された場合、処理できるように通知を受けたい(メッセージの表示など)。どうやってやるの?

答えて

0

私はこの問題についていくつかお手伝いします。サービスを作成するためのガット

bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback); 

に接続中に接続の場合 はあなたができ、また、あなたは「AutoConnectのオプションに 『』真を設定することができ、この

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 
      } 

と同じように扱うことができます破ります強制停止しない限り、それ自体を再起動するスティッキーサービスを作成します。

関連する問題