I'm playing with the Web Bluetooth API and I'm trying to get the current battery level of my device (Xiaomi MyBand, BLE device) from the browser.
I'm a little bit newbie with the bluetooth web api, keep in mind maybe I'm missing something basic.
My setup:
- Chrome Version 76.0.3809.132 (64 bits)
- macOS Mojave v10.14
This is my Javascript code, I call this function when I click a button:
navigator.bluetooth
.requestDevice({filters: [
{ name: 'MI' },
{ services: ['battery_service'] }
]})
.then(device => {
console.log(1)
return device.gatt.connect()
})
.then(server => {
console.log(2)
return server.getPrimaryService('battery_service')
})
.then(service => {
console.log(3)
return service.getCharacteristic('battery_level')
})
.then(characteristic => {
console.log(4)
return characteristic.readValue()
})
.then(value => {
console.log(5)
console.log(value.getUint8(0))
})
.catch(error => {
console.log('error: ', error)
})
I log number 2 and then I get this error:
Error: NotFoundError: No Services matching UUID 0000180f-0000-1000-8000-00805f9b34fb found in Device.
I'm stuck. Any idea about what I need to do?
Full example code is here: https://github.com/baumannzone/browser-apis/blob/master/src/views/Bluetooth/Example1.vue
Live Demo: https://browser-apis.netlify.com/#/bluetooth
Thanks you all.