title: Reading values from BLE device
body: <p>I'm owning a Polar H10 chest strap which operates with blueooth low energy and provides heart rate and heart rate variability.</p>
  
  <p>I want to read out these values with an Android app. I'm able to connect to the device thanks to the help in the <a href="https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#close" rel="nofollow noreferrer">official BLE tutorial</a>. The problem now is to read out the heart rate and heart rate variability values from the device. Everytime a new value is available on the device I want to read out this value (and there are at least every second new values).</p>
  
  <p>I have found the following pieces of code:</p>
  
  <pre><code>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 &amp; 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 &amp; 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 &amp; 0x08) != 0) {
              // calories present
              energy = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset);
              offset += 2;
              Log.d(TAG, "Received energy: {}"+ energy);
          }
          if ((flag &amp; 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 &gt; 0) {
                  Integer[] mRr_values = new Integer[rr_count];
                  for (int i = 0; i &lt; 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;
      }
  </code></pre>
  
  <p>How can I use it to extract the heart rate and r-r intervals (beat to beat intervals) assuming that I have a connection to the device? I would be happy if somebody could make a short example. Addtionally, I would like to use a service so that it runs in the background and I can do other work and it drains less battery. But the service should have highest priority so that it get not killed.</p>
  
  <p>In the <code>extractBeatToBeatInterval()</code> method there is a <code>int offset = 1;</code> with the description </p>
  
  <blockquote>
    <p>This depends on hear rate value format and if there is energy data</p>
  </blockquote>
  
  <p>I don't understand the meaning of this offest. What offset value should I use?</p>
  
  <p>Second, if the connection breaks I would like to get a notification so that I can handle it (e.g. displaying a message). How can I do that?</p>