Decoding iBeacon Advertising Using Javascript

iBeacon advertising consists of a UUID, major, minor and measured power. To decode the advertising using Javascript:

function decodeIBeacon(packet) {
  var uuid = packet.substring(4, 40);
  var major = parseInt(packet.substring(40, 44), 16);
  var minor = parseInt(packet.substring(44, 48), 16);
  var power = parseInt(packet.substring(48, 50), 16) - 256;

  var beacon = {
    uuid: uuid,
    major: major,
    minor: minor,
    power: power
  };

  return beacon;
}

This function takes a hexadecimal string representation of an iBeacon advertisement packet as input and decodes it into an object that contains the UUID, major and minor values and the measured power (measured in dBm) of the beacon.

To use this function, you need to extract the iBeacon advertisement packet from the Bluetooth Low Energy advertisement data received by the BLE scanning device. The iBeacon advertisement packet typically starts with a pattern similar to the following: 02 01 06 1a ff 4c 00 02 15. You can extract the iBeacon part by searching for these bytes in the advertisement data and then take the next 20 bytes as the packet.

View iBeacons