Web 蓝牙 API:不允许 Origin 访问该服务

faz*_*bbi 3 javascript bluetooth-lowenergy web-bluetooth

我正在使用网络蓝牙 API 来连接 BLE 设备。它在https://localhost上完美运行。但是当我在我的实时服务器上尝试它也是在 https 上或者当我在http://locahost上尝试时,它通过我这个错误“Origin is not allowed to access the service。提示:将服务 UUID 添加到 'optionalServices ' 在 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;
            });
        },
Run Code Online (Sandbox Code Playgroud)

Fra*_*ort 5

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