Web Bluetooth / Reset Energy Expended (Async Await) 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, Mac, and Windows 10.

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 (Promises) sample.

Live Output


JavaScript Snippet

async function onButtonClick() {
  try {
    log('Requesting Bluetooth Device...');
    const device  = await navigator.bluetooth.requestDevice({
        filters: [{services: ['heart_rate']}]});

    log('Connecting to GATT Server...');
    const server = await device.gatt.connect();

    log('Getting Heart Rate Service...');
    const service = await server.getPrimaryService('heart_rate');

    log('Getting Heart Rate Control Point Characteristic...');
    const characteristic = await service.getCharacteristic('heart_rate_control_point');

    log('Writing Heart Rate Control Point Characteristic...');
    // Writing 1 is the signal to reset energy expended.
    let resetEnergyExpended = Uint8Array.of(1);
    await characteristic.writeValue(resetEnergyExpended);

    log('> Energy expended has been reset.');
  } catch(error) {
    log('Argh! ' + error);
  }
}