最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Web Bluetooth API: Origin is not allowed to access the service - Stack Overflow

programmeradmin2浏览0评论

I am using web Bluetooth API, to connect a BLE device. Its working perfect on https://localhost. But when I try it on my live server which is also on https or when I try it on http://locahost, it throughs me this error " Origin is not allowed to access the service. Tip: Add the service UUID to 'optionalServices' in requestDevice() options.". The code is given below. I have already added 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;
            });
        },

I am using web Bluetooth API, to connect a BLE device. Its working perfect on https://localhost. But when I try it on my live server which is also on https or when I try it on http://locahost, it throughs me this error " Origin is not allowed to access the service. Tip: Add the service UUID to 'optionalServices' in requestDevice() options.". The code is given below. I have already added 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;
            });
        },
Share Improve this question edited Oct 7, 2019 at 23:59 Fitzi 1,67311 silver badges18 bronze badges asked Oct 7, 2019 at 6:59 fazalerabbifazalerabbi 1731 gold badge6 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It seems like it is missing an "s" to optionalService. It should be optionalServices according to https://webbluetoothcg.github.io/web-bluetooth/#dom-requestdeviceoptions-optionalservices

Download nRF app from playstore and connect to the BLE device and after connection you can get all the Primary Services and its UUID. Get the Primary Service UUID and place it on the optionalServices[0xFEE0] and get Primary Services Characterstics (listed inside primary services ) and place on service.getCharacteristic(0x2A2B).

These codes are of MI BAND to get time and date.

发布评论

评论列表(0)

  1. 暂无评论