0

我一直在摆弄新的网络蓝牙功能。我有这些估计信标之一:http: //developer.estimote.com/

我知道我的信标的 uuid。这是我正在使用的代码(它是一个角度应用程序,因此是 $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});
    }
4

2 回答 2

2

遗憾的是,该watchAdvertisements方法尚未实施。您可能需要查看https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md上的实施状态页面,以了解 Chrome 和其他浏览器何时支持此方法。

于 2017-04-28T13:40:36.080 回答
0

目前还不清楚问题是什么,但这里有一些提示:

  1. 了解网络蓝牙 API 是一组正在积极开发中的拟议标准,并且支持仅限于 Google Chrome 的某些版本,并作为 Noble.js 蓝牙中央模块的垫片。如果您使用的是 Angular,则需要使用后一个 shim 来使其工作,也许您已经这样做了。您可以在此处阅读更多信息: https ://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web

  2. 如果您正在使用该interpretIBeacon功能,那么只需解析字节即可,您似乎正在做这件事。您可以在我的回答中看到有关信标字节布局的更多信息:https ://stackoverflow.com/a/19040616/1461050

  3. 您不想将信标 UUID 过滤为服务,因此您需要删除optionalServices: ['b9407f30-f5f8-466e-aff9-25556b57fe6d']. 信标 ProximityUUID 与 GATT ServiceUUID 不同,即使它们表面上具有相同的格式。您正在寻找的制造商广告类型的信标蓝牙广告,而不是 *GATT 服务** 广告。这两种广告类型不同,但上面显示的 API 应该返回两种类型的结果。

于 2017-04-28T13:39:27.990 回答