title: Get Tx Power of BLE Beacon in Android
body: <p>I want to get the Tx power of a BLE beacon with an Android device.</p>
  
  <p>I defined the assigned number for Tx power here.</p>
  
  <pre><code>public class AssignedNumbers {
      ...
      public static final byte TXPOWER = 0x0A;
      ...
  }
  </code></pre>
  
  <p>Then I made a function to get Tx power here.</p>
  
  <pre><code>public class AdvertisingData {
      ...
      public static Integer getTxPowerLevel(byte[] scanRecord) {
      // Check for BLE 4.0 TX power
      int pos = findCodeInBuffer(scanRecord, AssignedNumbers.TXPOWER);
      if (pos &gt; 0) {
        return Integer.valueOf(scanRecord[pos]);
      }
      return null;
      }
      ...
      private static int findCodeInBuffer(byte[] buffer, byte code) {
          final int length = buffer.length;
          int i = 0;
          while (i &lt; length - 2) {
              int len = buffer[i];
              if (len &lt; 0) {
                  return -1;
              }
  
              if (i + len &gt;= length) {
                  return -1;
              }
  
              byte tcode = buffer[i + 1];
              if (tcode == code) {
                  return i + 2;
              }
  
              i += len + 1;
          }
  
          return -1;
      }
      ...
  }
  </code></pre>
  
  <p>Finally, I put a line of code to check the Tx power.</p>
  
  <pre><code>private BluetoothAdapter.LeScanCallback mLeScanCallback = 
          new BluetoothAdapter.LeScanCallback() {
              ...
              System.out.println("TxPower: " + AdvertisingData.getTxPowerLevel(scanRecord));
              ...
  };
  </code></pre>
  
  <p>However, the result is shown as below.</p>
  
  <blockquote>
    <p>04-22 16:34:14.249: I/System.out(29133): TxPower: null</p>
  </blockquote>
  
  <p>The log of onScanResult() is</p>
  
  <blockquote>
    <p>04-22 16:34:14.247: D/BluetoothLeScanner(29133): onScanResult() -
    ScanResult{mDevice=90:59:AF:0F:31:01, mScanRecord=ScanRecord
    [mAdvertiseFlags=6, mServiceUuids=null, mManufacturerSpecificData={76=[2, 21,
    -43, 117, 98, 71, 87, -94, 67, 68, -111, 93, -107, -103, 73, 121, 64, -89, 0,
    0, 0, 0, -60]}, mServiceData={0000180a-0000-1000-8000-00805f9b34fb=[1, 49, 15,
    -81, 89, -112, -60, 0, 0, 0, 0]}, mTxPowerLevel=-2147483648,
    mDeviceName=pebBLE], mRssi=-79, mTimestampNanos=8204624857836}</p>
  </blockquote>
  
  <p>How to get the right value of the Tx power? The values should be 4, 0, or -23(dBm).</p>