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!');
}
});
});
✓ 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
device = new Device()
to instantiate a new bluetooth deviceaccessor = device.register('service','characteristic')
to get access to the desired characteristic
within service
, both names and UIDs are supportedaccessor()
to read and accessor(value)
to write, they both return a promiseaccessor.getCharacteristic()
to get access to the characteristic object, useful if you need to subscribe to updates from the characteristicCheck your browser support for web bluetooth before running.
I made a few slides to explain and detail the examples, you can find them on slides.com.