API веб Bluetooth: Origin не имеет доступа к сервису - PullRequest
0 голосов
/ 07 октября 2019

Я использую веб-API Bluetooth для подключения устройства BLE. Он отлично работает на https://localhost. Но когда я пробую его на своем живом сервере, который также находится на https, или когда я пробую на http://locahost,, я получаю эту ошибку: «Origin не имеет доступа к сервисуПодсказка: добавьте UUID службы в 'AdditionalServices' в параметрах requestDevice (). "Код приведен ниже. Я уже добавил дополнительные услуги.

      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;
            });
        },

1 Ответ

2 голосов
/ 08 октября 2019

Кажется, что отсутствует "s" для optionalService. Это должно быть optionalServices в соответствии с https://webbluetoothcg.github.io/web-bluetooth/#dom-requestdeviceoptions-optionalservices

...