SoFunction
Updated on 2025-03-04

How to detect IBeacon hotspots on Android

IBeacon is a type of BLE. The key to searching for iBeacon base stations is to scan the scanRecord array scanned by the device to identify whether there are two numbers 02 15 in bold italics below. If so, the Bluetooth device you searched for is IBeacon.
// AirLocate:
// 02 01 1a 1a ff 4c 00 02 15 # Apple's fixed iBeacon advertising prefix
// e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # iBeacon profile uuid
// 00 00 # major
// 00 00 # minor
// c5 # The 2's complement of the calibrated Tx Power

The following steps are used to detect IBeacon hotspots.

1. Obtain Bluetooth control permission for mobile phones

Write in the manifest file:

<uses-permission android:name=""/>
<uses-permission android:name=".BLUETOOTH_ADMIN"/>

2. Check whether the phone supports Bluetooth and obtain the mBluetoothAdapter object

if (!getPackageManager().hasSystemFeature(
        PackageManager.FEATURE_BLUETOOTH_LE))
    {
      (this, .ble_not_supported, Toast.LENGTH_SHORT)
          .show();
      finish();
    }
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = ();

    if (mBluetoothAdapter == null)
    {
      (this, .error_bluetooth_not_supported,
          Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

3. Implement the LeScanCallback callback interface

Every time the device detects a Bluetooth device, it will call back the onLeScan() method in this interface and pass in the scanned device, rssi, scanRecord and other parameters.

private  mLeScanCallback = new ()
  {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi,
        byte[] scanRecord)
    {
      // Process the scanned parameters here      //Discern whether it is an IBeacon device and do the corresponding processing.    }
  };

4. Methods for processing scanned parameters

public class iBeaconClass
{

  static public class iBeacon
  {
    public String name;
    public int major;
    public int minor;
    public String proximityUuid;
    public String bluetoothAddress;
    public int txPower;
    public int rssi;
  }


  /**
    * Pass the scanned information into this method
    * This method will determine whether the scanned device is IBeacon
    * If so, return an IBeacon object
    * If not, return null
    * @param device
    * @param rssi
    * @param scanData
    * @return
    */
  public static iBeacon fromScanData(BluetoothDevice device, int rssi,
      byte[] scanData)
  {

    int startByte = 2;
    boolean patternFound = false;
    while (startByte &lt;= 5)
    {
      if (((int) scanData[startByte + 2] &amp; 0xff) == 0x02
          &amp;&amp; ((int) scanData[startByte + 3] &amp; 0xff) == 0x15)
      {
        // yes! This is an iBeacon
        patternFound = true;
        break;
      } else if (((int) scanData[startByte] &amp; 0xff) == 0x2d
          &amp;&amp; ((int) scanData[startByte + 1] &amp; 0xff) == 0x24
          &amp;&amp; ((int) scanData[startByte + 2] &amp; 0xff) == 0xbf
          &amp;&amp; ((int) scanData[startByte + 3] &amp; 0xff) == 0x16)
      {
        iBeacon iBeacon = new iBeacon();
         = 0;
         = 0;
         = "00000000-0000-0000-0000-000000000000";
         = -55;
        return iBeacon;
      } else if (((int) scanData[startByte] &amp; 0xff) == 0xad
          &amp;&amp; ((int) scanData[startByte + 1] &amp; 0xff) == 0x77
          &amp;&amp; ((int) scanData[startByte + 2] &amp; 0xff) == 0x00
          &amp;&amp; ((int) scanData[startByte + 3] &amp; 0xff) == 0xc6)
      {

        iBeacon iBeacon = new iBeacon();
         = 0;
         = 0;
         = "00000000-0000-0000-0000-000000000000";
         = -55;
        return iBeacon;
      }
      startByte++;
    }

    if (patternFound == false)
    {
      // This is not an iBeacon
      return null;
    }

    iBeacon iBeacon = new iBeacon();

     = (scanData[startByte + 20] &amp; 0xff) * 0x100
        + (scanData[startByte + 21] &amp; 0xff);
     = (scanData[startByte + 22] &amp; 0xff) * 0x100
        + (scanData[startByte + 23] &amp; 0xff);
     = (int) scanData[startByte + 24]; // this one is signed
     = rssi;

    // AirLocate:
    // 02 01 1a 1a ff 4c 00 02 15 # Apple's fixed iBeacon advertising prefix
    // e2 c5 6d b5 df fb 48 d2 b0 60 d0 f5 a7 10 96 e0 # iBeacon profile
    // uuid
    // 00 00 # major
    // 00 00 # minor
    // c5 # The 2's complement of the calibrated Tx Power

    // Estimote:
    // 02 01 1a 11 07 2d 24 bf 16
    // 394b31ba3f486415ab376e5c0f09457374696d6f7465426561636f6e00000000000000000000000000000000000000000000000000

    byte[] proximityUuidBytes = new byte[16];
    (scanData, startByte + 4, proximityUuidBytes, 0, 16);
    String hexString = bytesToHexString(proximityUuidBytes);
    StringBuilder sb = new StringBuilder();
    ((0, 8));
    ("-");
    ((8, 12));
    ("-");
    ((12, 16));
    ("-");
    ((16, 20));
    ("-");
    ((20, 32));
     = ();

    if (device != null)
    {
       = ();
       = ();
    }

    return iBeacon;
  }

  private static String bytesToHexString(byte[] src)
  {
    StringBuilder stringBuilder = new StringBuilder("");
    if (src == null ||  &lt;= 0)
    {
      return null;
    }
    for (int i = 0; i &lt; ; i++)
    {
      int v = src[i] &amp; 0xFF;
      String hv = (v);
      if (() &lt; 2)
      {
        (0);
      }
      (hv);
    }
    return ();
  }
}

5. Turn on Bluetooth

();

6. Start scanning

(mLeScanCallback);

The code is changed fromLink address

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.