Search code examples
javascriptbluetooth-lowenergyibeaconnavigator

BLE Advertisement scan in javascript


Scanning of advertisements from nearby Bluetooth Low Energy Devices is my priority. The code which i tried,requested the devices(it wanted to pair to get the characteristics). But my use case was to listen to their advertisements. For which I found this code

function recordNearbyBeacon(major, minor, pathLossVs1m) { ... }
navigator.bluetooth.requestLEScan({
  filters: [{manufacturerData: {0x004C: {dataPrefix: new Uint8Array([
    0x02, 0x15, // iBeacon identifier.
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15  // My beacon UUID.
  ])}}}],
  options: {
    keepRepeatedDevices: true,
  }
}).then(() => {
  navigator.bluetooth.addEventListener('advertisementreceived', event => {
    let appleData = event.manufacturerData.get(0x004C);
    if (appleData.byteLength != 23) {
      // Isn’t an iBeacon.
      return;
    }
    let major = appleData.getUint16(18, false);
    let minor = appleData.getUint16(20, false);
    let txPowerAt1m = -appleData.getInt8(22);
    let pathLossVs1m = txPowerAt1m - event.rssi;

    recordNearbyBeacon(major, minor, pathLossVs1m);
  });
})

There are certain uuids in the official site which i tried but none seem to work. I want to get the characteristic values from the advertisement data. How to get the data in Javascript? Thanks in advance.


Solution

  • According to the implementation status document from the Web Bluetooth GitHub, the Web Bluetooth Scanning API (which is what provides the requestLEScan method) is not yet implemented in any browser.