standard bluetooth services in espruino ?

Posted on
  • Hi

    I'm new to espruino and I wondered if there was a way to expose the standard bluetooth services, for example the Battery Service ?

    I wish to use the scriptability for example to send IR codes when a temperature is met however don't want to step away from tooling/libraries for interacting with standard bluetooth.

    How would this best be achieved ?

    Thanks

    John Jones

  • Hi,

    Yes, it's really easy to get Espruino to advertise standard services - you just look up the service you want on the bluetooth site: https://www.bluetooth.com/specifications­/gatt/services

    In this case: https://www.bluetooth.com/specifications­/gatt/viewer?attributeXmlFile=org.blueto­oth.service.battery_service.xml

    Then you just use setServices/updateServices with the info from that page, so something like:

    NRF.setServices({
      0x180F : {
        0x2A19 : {
          value : Puck.getBatteryPercentage(),  readable : true
        }
      }
    });
    
    setInterval(function() {
      NRF.updateServices({
        0x180F : {
          0x2A19 : {
           value : Puck.getBatteryPercentage()
          }
        }
      });
    }, 60000);
    
  • Hello there, I'm building an Android app with Gatt connection and receiving data from puck.
    I want to know how to advertise data with byte and value?? I want to advertise temperature, light, battery and a state(true, false). How can I do that like the example above? Can I send them all together or I have to advertise them separately?
    Thanks in advance!

  • Yes, you can send them all together if you want...

    var myState = 0;
    
    function getMyData() {
      return [E.getTemperature(),Puck.light()*256, Puck.getBatteryPercentage(),myState];
    }
    
    NRF.setServices({
      0x1234 : {
        0x5678 : {
          value :  getMyData(),  readable : true
        }
      }
    });
    setInterval(function() {
      NRF.updateServices({
        0x1234 : {
          0x5678 : {
           value : getMyData()
          }
        }
      });
    }, 60000);
    

    And then you can just unpack them at the other end.

    Although this post was originally about standard services/characteristics. If you're sending stuff like that then what you've got definitely isn't a standard service and you'll probably want to look at using your own 128 bit UUIDs for it.

  • What if I want to send them separately? For example i want to send 2 bytes, the first is the number that will help me to know what value comes after it(temp, light, etc..). Because in android if i send them all together they are displayed in two different rows.. thanks!

  • I'm not sure I understand the problem - you should just get a byte array from Android, and element 0 will be temperature, 1 will be light, etc. If you can read out the first byte to see what the data is, and the second byte to read the data itself then you're basically all the way to decoding everything.

    You can always do:

    function getMyData() {
      return [
        0,E.getTemperature(),
        1,Puck.light()*256, 
        2,Puck.getBatteryPercentage(),
        3,myState];
    }
    

    But if you want to send multiple different characteristics that you can read separately then that's what having multiple different characteristics is for...

    NRF.setServices({
      0x1234 : {
        0x5678 : {  value :  [E.getTemperature()],  readable : true  },
        0x5679 : {  value :  [Puck.light()*256],  readable : true  },
        0x567A : {  value :  [Puck.getBatteryPercentage()],  readable : true  },
        0x567B : {  value :  [myState],  readable : true  },
      }
    });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

standard bluetooth services in espruino ?

Posted by Avatar for johnjones @johnjones

Actions