Web Bluetooth / Reset Energy Expended Sample

Available in Chrome 45+ | View on GitHub | Browse Samples

The Web Bluetooth API lets websites discover and communicate with devices over the Bluetooth 4 wireless standard using the Generic Attribute Profile (GATT). It is currently partially implemented in Android M, Chrome OS, Linux, and Mac.

This sample illustrates the use of the Web Bluetooth API to reset energy expended from a nearby Bluetooth Device advertising Heart Rate with Bluetooth Low Energy. You may want to try this demo with the BLE Peripheral Simulator App from the Google Play Store and check out the Reset Energy (Async Await) sample.

Live Output


JavaScript Snippet

function onButtonClick() {
  log('Requesting Bluetooth Device...');
  navigator.bluetooth.requestDevice({filters: [{services: ['heart_rate']}]})
  .then(device => {
    log('Connecting to GATT Server...');
    return device.gatt.connect();
  })
  .then(server => {
    log('Getting Heart Rate Service...');
    return server.getPrimaryService('heart_rate');
  })
  .then(service => {
    log('Getting Heart Rate Control Point Characteristic...');
    return service.getCharacteristic('heart_rate_control_point');
  })
  .then(characteristic => {
    log('Writing Heart Rate Control Point Characteristic...');

    // Writing 1 is the signal to reset energy expended.
    let resetEnergyExpended = Uint8Array.of(1);
    return characteristic.writeValue(resetEnergyExpended);
  })
  .then(_ => {
    log('> Energy expended has been reset.');
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}