【问题标题】:Web Bluetooth API: Origin is not allowed to access the serviceWeb蓝牙API:Origin不允许访问服务
【发布时间】:2020-01-12 19:15:42
【问题描述】:

我正在使用网络蓝牙 API 来连接 BLE 设备。它在https://localhost 上完美运行。但是,当我在也位于 https 上的实时服务器上尝试它时,或者当我在 http://locahost 上尝试它时,它会通过我这个错误“Origin is not allowed to access the service. Tip: Add the service UUID to 'optionalServices' in requestDevice() 选项。”。代码如下。我已经添加了 optionalServices。

      scanDevices () {
            if(navigator.bluetooth) {
                navigator.bluetooth.requestDevice({
                    acceptAllDevices: true,
                    optionalService: ['0000fee0-0000-1000-8000-00805f9b34fb', '0000fee1-0000-1000-8000-00805f9b34fb']
                })
                .then(device => {
                    // save the device returned so you can disconnect later:
                    this.device = device;
                    this.device.addEventListener('gattserverdisconnected', this.onDisconnected);
                    // connect to the device once you find it:
                    return this.connect();
                })
                .then((server) => {
                    this.server = server;
                    return server;
                })
                .catch(function(error) {
                    // catch any errors:
                    console.error('Connection failed!', error);
                });
            } else {
                this.errorMessage = 'Your browser does not support web bluetooth API.';
            }

        },
        connect(){
            let connection = this.device.gatt.connect();
            console.log('Connected', connection);
            return connection;
        },
        readData (){
            this.isLoader = true;
            console.log('get the primary service:');
            console.log(this.server);
            this.server.getPrimaryService(this.parsedService)
            .then((service) => {
                console.log('get the  characteristic:');
                return service.getCharacteristic(this.parsedCharacteristic);
            })
            .then(characteristic => {
                return characteristic.readValue();
            })
            .then(value => {
                console.log(value);
                this.isLoader = false;
                let decoder = new TextDecoder('utf-8');
                console.log(decoder.decode(value));
            })
            .catch(error => {
                this.isLoader = false;
                this.errorMessage = error.message;
            });
        },

【问题讨论】:

    标签: javascript bluetooth-lowenergy web-bluetooth


    【解决方案1】:

    optionalService 似乎缺少一个“s”。根据https://webbluetoothcg.github.io/web-bluetooth/#dom-requestdeviceoptions-optionalservices应该是optionalServices

    【讨论】:

      【解决方案2】:

      从 playstore 下载 nRF 应用程序并连接到 BLE 设备,连接后您可以获得所有 Primary Services 及其 UUID。获取 Primary Service UUID 并将其放在 optionalServices[0xFEE0] 并获取 Primary Services Characterstics(在 primary services 中列出)并放在 service.getCharacteristic(0x2A2B)

      这些代码是 MI BAND 的,用于获取时间和日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多