Blighty
Blighty

Reputation: 199

Web bluetooth: How do I detect iBeacon?

I have been fiddling with the new web bluetooth functionality. I have one of these estimote beacons: http://developer.estimote.com/

I know the uuid for my beacon. Here is the code I am using(it is an angular app, hence $scope, $window):

$scope.runBT = runBT;
    function runBT() {
        let mobile = getMobileOperatingSystem();
        if (mobile === 'Android' || mobile === 'iOS') {
            $window.navigator.bluetooth.requestDevice({
                acceptAllDevices: true,
                optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']
            })
                .then(device => {
                    console.log('FOUND DEVICE: ', device);
                    device.watchAdvertisements();
                    device.addEventListener('advertisementreceived', interpretIBeacon);
                })
                .catch(error => { console.log(error); });
        }
    }

    function interpretIBeacon(event) {
        var rssi = event.rssi;
        var appleData = event.manufacturerData.get(0x004C);
        if (appleData.byteLength != 23 ||
            appleData.getUint16(0, false) !== 0x0215) {
            console.log({isBeacon: false});
        }
        var uuidArray = new Uint8Array(appleData.buffer, 2, 16);
        var major = appleData.getUint16(18, false);
        var minor = appleData.getUint16(20, false);
        var txPowerAt1m = -appleData.getInt8(22);
        console.log({
            isBeacon: true,
            uuidArray,
            major,
            minor,
            pathLossVs1m: txPowerAt1m - rssi});
    }

Upvotes: 0

Views: 2783

Answers (2)

François Beaufort
François Beaufort

Reputation: 5629

Sadly the watchAdvertisements method is not implemented yet. You may want to check the Implementation Status page at https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md to know when this method will be supported in Chrome and other browsers.

Upvotes: 2

davidgyoung
davidgyoung

Reputation: 64916

It's unclear what the problem is, but here are a few tips:

  1. Understand that the web bluetooth APIs are a proposed set of standards under active development, and support is limited to certain builds of Google Chrome, and as a shim for the Noble.js bluetooth central module. If you are using Angular, you need to use the latter shim to make it work, which perhaps you already are. You can read more here: https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web

  2. If you are getting as far as the interpretIBeacon function, then it's just a matter of parsing the bytes out, which you seem well on your way to doing. You can see more about the byte layout of the beacon in my answer here: https://stackoverflow.com/a/19040616/1461050

  3. You don't want to filter for the beacon UUID as a service, so you need to remove optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']. A beacon ProximityUUID is not the same as a GATT ServiceUUID even though they superficially have the same format. A beacon bluetooth advertisement of the type you are looking for a manufacturer advertisement and not a *GATT service** advertisement. The two advertisement types are different, but the APIs shown above should return results from both types.

Upvotes: 0

Related Questions