admin管理员组文章数量:1406007
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
2 Answers
Reset to default 5It 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.
本文标签: javascriptWeb Bluetooth API Origin is not allowed to access the serviceStack Overflow
版权声明:本文标题:javascript - Web Bluetooth API: Origin is not allowed to access the service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744966785a2634996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论