Get battery percentage over web bluetooth

Posted on
  • Hi! I'm trying to read data from the puck.js over web bluetooth. As a PoC I just wanted to read the battery percentage, but it doesn't work: The Puck.js does not appear on the list for the bluetooth devices in the web browser. My assumption is, that the web bluetooth library does not know, that my puck is advertising battery data.. Any idea how to fix this?

    The code on my Puck.js:

    NRF.setServices({
      0x180F : { // Battery Service
        0x2A19: {  // Battery Level
          readable: true,
          notify: true,
          value : [Puck.getBatteryPercentage()]
    }}});
    setInterval(function(){
      NRF.updateServices({
        0x180F: {
          0x2A19: {
            value : [Puck.getBatteryPercentage()]
          }
        }
      });
    }, 60000);
    

    For testing purposes I used the Web Bluetooth Sample page: https://googlechrome.github.io/samples/web-bluetooth/battery-level.html

  • Hi! Actually someone else had exactly this problem recently!

    The issue is that setServices sets the services that are available once you're connected to the device. There's another type of services - advertised services - that are broadcast to everyone even when not connected, and it's those that Web Bluetooth is looking for.

    It's actually in the second half of the setServices docs: https://www.espruino.com/Reference#l_NRF_setServices

    Something like this should fix it:

    NRF.setServices({
      0x180F : { // Battery Service
        0x2A19: {  // Battery Level
          readable: true,
          notify: true,
          value : [Puck.getBatteryPercentage()]
    }}},{
      advertise: [ '180F' ]
    });
    

    Out of interest, any thoughts about where I could put some info on this so others are less likely to fall into the same trap :)

  • Hi Gordon!

    Thanks, this works. :-) Regarding where to put some info on this: It would be great to have a tutorial, using the Puck.js together with the web bluetooth API (not only with your wrapper class).

    Now, I have some other questions regarding this:

    1. Do you know if it's possible to connect multiple devices at once via Web Bluetooth?
    2. Do you know if I can connect to devices automatically via Web Bluetooth? (Let's say: Remember a device and if it's reachable, Web Bluetooth should automatically connect to it)
    3. Using the characteristicvaluechanged event, I should be able to get the real time data, every time the battery percentage changes, right?
  • Thanks - yes, I'll have a go at that :)

    Do you know if it's possible to connect multiple devices at once via Web Bluetooth?
    It is, yes! It depends on your platform, but it's around 5 or 6 maximum

    Do you know if I can connect to devices automatically via Web Bluetooth? (Let's say: Remember a device and if it's reachable, Web Bluetooth should automatically connect to it)

    I don't believe so - yet. That's one of the things they're looking into though.

    Using the characteristicvaluechanged event, I should be able to get the real time data, every time the battery percentage changes, right?

    Yes, absolutely - as long as notify:true in that Puck.js service :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Get battery percentage over web bluetooth

Posted by Avatar for Tineler @Tineler

Actions