/jsgatt

JSGatt adds some syntactic sugar on top of the web bluetooth api

Primary LanguageJavaScriptMIT LicenseMIT

Build Status styled with prettier

JSGatt

JSGatt adds some syntactic sugar on top of the web bluetooth api.

import Device from 'jsgatt';

const phone = new Device();
const battery = phone.register(
  'battery_service', // service name or UID
  'battery_level' // characteristic name or UID
);

document.querySelector('button').addEventListener('click', function(){
  battery().then(value => {
    if (value.getUint8(0) < 20) {
      alert('low battery!');
    }
  });
});

Features

✓ shorter syntax
✓ auto reconnection
✓ caches characteristics
✓ aggregates services for the same device
✓ prevents writing if another writing is in progress
✓ handles accessing a characteristic while the device is still connecting

API

  • use device = new Device() to instantiate a new bluetooth device
  • use accessor = device.register('service','characteristic') to get access to the desired characteristic within service, both names and UIDs are supported
  • use accessor() to read and accessor(value) to write, they both return a promise
  • use accessor.getCharacteristic() to get access to the characteristic object, useful if you need to subscribe to updates from the characteristic

Examples

Check your browser support for web bluetooth before running.

Slides

I made a few slides to explain and detail the examples, you can find them on slides.com.