1

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: Chrome console

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.

Baumannzone
  • 760
  • 2
  • 19
  • 38
  • 2
    Is the service there? Confirm with a tool such as chrome://bluetooth-internals or nRF Connect. https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#dev_tips – Vincent Scheib Sep 11 '19 at 07:27
  • 1
    Also, try this generic battery level reading sample, known to work with other devices: https://googlechrome.github.io/samples/web-bluetooth/battery-level-async-await.html – Vincent Scheib Sep 11 '19 at 07:28
  • Hey, thanks for the reply. Yes, it's listed in chrome BT internals, see: https://i.imgur.com/l4YGHSR.png. I just want to get some device information, I don't need specifically battery. I just want to demonstrate I can read something of the device from the browser (not only the name) – Baumannzone Sep 11 '19 at 11:19
  • 1
    I do not see the service ...180f... in that screen shot. You have expanded service ...fee0... (registered assigned number, found in https://www.bluetooth.com/specifications/assigned-numbers/16-bit-uuids-for-members/ ) . Again, the device likely isn't exposing that service (even if it advertises it). Try reading other service/characteristics it does expose. A search finds some info at https://github.com/nikita-graf/mi-band-1s-web-bluetooth which may help. – Vincent Scheib Sep 12 '19 at 22:46

0 Answers0