FrogInABox
5/30/2017 - 1:34 PM

Hat tip to [http://harald.studiokubota.com/](http://harald.studiokubota.com/wordpress/index.php/2017/03/20/puck-js-and-bluetooth-le/)

// Have a service to show temperature and battery level
// This is only visible when you are connecting to the Puck

var currentTemperature=E.getTemperature().toFixed(2)*100;

console.log("Temp: "+currentTemperature);

NRF.setLowPowerConnection(true);

NRF.setServices({
 "12411007-877B-4B6E-870C-1D8A036A956A" : { // Health Thermometer
  "7E3DEFD9-1426-068F-D744-FA70603D83AC": { // Temperature
   readable: true,
   notify: true,
   value : [ currentTemperature % 256, currentTemperature / 256 ]
  }
 },
 0x180f : { // Battery Level
  0x2a19 : { // Percentage
   readable : true,
   notify: true,
   value: [ Puck.getBatteryPercentage() ],
  }
 }
});

// Updating the temperature
setWatch(function() {
 currentTemperature += 1; // For debugging: increase temp by 0.01 degree per update
 console.log(currentTemperature);
 NRF.updateServices({
  "12411007-877B-4B6E-870C-1D8A036A956A" : {
   "7E3DEFD9-1426-068F-D744-FA70603D83AC" : {
    value : [ currentTemperature % 256, currentTemperature / 256 ],
    notify: true
   }
  }
 });
}, BTN, {edge:"rising", debounce:50, repeat:true});