title: Android: iBeacon - read its advertisement (e.g txPower)
body: <p>I have a problem here right now as I'm trying to read the tx-Power from my Onyx iBeacon. I've read multiple suggestions and tried so many code on the internet, but I still can't figure this out.</p>
  
  <p>I'm using android lollipop and tried the iBeacon tutorial from <a href="https://github.com/devunwired/accessory-samples/tree/master/BluetoothGatt" rel="nofollow noreferrer">https://github.com/devunwired/accessory-samples/tree/master/BluetoothGatt</a>
  for a Bluetooth application which extracts the <code>BLUETOOTH_THERM_SERVICE</code>.</p>
  
  <p>I found out two values for tx-Power from advertisement:
  <a href="https://stackoverflow.com/questions/24594287/bluetooth-le-tx-power-reading">Bluetooth LE TX Power reading</a></p>
  
  <p><code>0x1804</code> and its characteristic <code>0x2A07</code>, so I adapted the Parcel UUID to filter my ScanResults with <code>0x1804</code>:</p>
  
  <pre><code>    public static final ParcelUuid THERM_SERVICE = ParcelUuid.fromString("00001809-0000-1000-8000-00805f9b34fb");
      public static final ParcelUuid TX_POWER_SERVICE = ParcelUuid.fromString("00001804-0000-1000-8000-00805F9B34FB");
      public static final ParcelUuid ONYX_STANDARD_SERVICE = ParcelUuid.fromString("0000180a-0000-1000-8000-00805f9b34fb");
  </code></pre>
  
  <p>As described in the tutorial:</p>
  
  <pre><code> byte[] data = record.getServiceData(TX_POWER_SERVICE);
  </code></pre>
  
  <p>Should store a byte array but it always returns null! The only thing that stores a byte array, but I think it stores the whole record is: </p>
  
  <pre><code>byte[] data = record.getServiceData(ONYX_STANDARD_SERVICE);
  </code></pre>
  
  <p>Trying to move on and use the data I could get with <code>ONYX_STANDARD_SERVICE</code> I can see a scanResult:</p>
  
  <pre><code> onScanResult() - ScanResult{mDevice=78:A5:04:07:AE:96, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21, 32, -54, -24, -96, -87, -49, 17, -29, -91, -30, 8, 0, 32, 12, -102, 102, 0, 7, -82, -106, -78]}, mServiceData={0000180a-0000-1000-8000-00805f9b34fb=[120, -91, 4, 7, -82, -106, -78, 32, -54, -24, -96, 0, 7, -82, -106]}, mTxPowerLevel=-2147483648, mDeviceName=OnyxBeacon], mRssi=-63, mTimestampNanos=174691272168825}
  </code></pre>
  
  <p>I think the important part of my record is:</p>
  
  <pre><code> mServiceData={0000180a-0000-1000-8000-00805f9b34fb=[120, -91, 4, 7, -82, -106, -78, 32, -54, -24, -96, 0, 7, -82, -106]}
  </code></pre>
  
  <p>How do I extract my txPower value from this ServiceData? I checked on several applications that my tx-power is -78.</p>
  
  <p>By checking the result with ONYX_STANDARD_SERVICE I can get this value with <code>scanResult[6] = -78</code>.</p>
  
  <p>Is this the right way to do it, and if so why?</p>
  
  <p>I also found some interesting examples on this issue where another value is mentioned to read from record:</p>
  
  <p><a href="https://github.com/google/uribeacon/blob/master/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/scan/compat/ScanRecord.java" rel="nofollow noreferrer">https://github.com/google/uribeacon/blob/master/android-uribeacon/uribeacon-library/src/main/java/org/uribeacon/scan/compat/ScanRecord.java</a></p>
  
  <p>If you look in here you'll find the code:</p>
  
  <pre><code>private static final int DATA_TYPE_TX_POWER_LEVEL = 0x0A;
  </code></pre>
  
  <p>Which one of these (<code>0x1804</code>,<code>0x2A07</code>,<code>0x0A</code>) is the right one?</p>
  
  <p>It would also be nice but not necessary that someone could explain me how the example code from getting <code>THERM_SERVICE</code> works:</p>
  
  <pre><code>private float parseTemp(byte[] serviceData) {
          /*
           * Temperature data is two bytes, and precision is 0.5degC.
           * LSB contains temperature whole number
           * MSB contains a bit flag noting if fractional part exists
           */
          float temp = (serviceData[0] &amp; 0xFF);
          if ((serviceData[1] &amp; 0x80) != 0) {
              temp += 0.5f;
          }
  
          return temp;
      }
  </code></pre>
  
  <p>Please help me on reading advertisement data and extracting tx-Power.</p>