jsgatt
v1.0.0
Published
wrapper around the browser web bluetooth api.
Downloads
19
Maintainers
Readme
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(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 desiredcharacteristic
withinservice
, both names and UIDs are supported - use
accessor()
to read andaccessor(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.
- Toggle and change color of a smart light - demo, video, source to run this example you'll need an Awox smartlight.
- Use the webcam to set the color from a smart light - demo, video, source to run this example you'll need an Awox smartlight.
- Toggle a smart light using the voice - demo, video, source to run this example you'll need an Awox smartlight.
- Subscribe to the battery level service - demo, video, source you can emulate a battery level service using any Android device with the BLE peripheal simulator app
- Drive a Parrot mini drone with the keyboard - demo, source to run this example you'll need a Parrot Minidrone. Massive thanks to poshaughnessy and voodootikigod for the original codebase.
Slides
I made a few slides to explain and detail the examples, you can find them on slides.com.