Peter O'Shaughnessy
0xff === 255
parseInt('ff', 16) === 255
// Length of 12 bytes
let buffer = new ArrayBuffer(12);
// ...Read data into the buffer...
let array = new Uint8Array(buffer);
// Gives e.g. 255 / 0xff:
let my8BitInt = array[0];
let buffer = new ArrayBuffer(12);
let dataView = new DataView(buffer);
// Gives e.g. 255 / 0xff:
let my8BitInt = dataView.getUint8(0);
Chrome Beta or Chrome Dev (Chrome stable soon)
Register at: bit.ly/WebBluetoothOriginTrial
More info: bit.ly/OriginTrials
Watch this space...
navigator.bluetooth.requestDevice({
filters: [{
name: 'SomeAmazingRobot'
}],
optionalServices: ['battery_service']
})
...
...
.then(device => device.connectGATT())
.then(server => server.getPrimaryService('batt_service'))
.then(service => service.getCharacteristic('batt_level'))
.then(characteristic => {
// Read battery level
return characteristic.readValue();
})
.then(value => {
let batteryLevel = value.getUint8(0);
// Or you could do:
// let array = new Uint8Array(value.buffer);
// batteryLevel = array[0];
console.log(`Battery level: ${batteryLevel}`);
});
A discovery service for 'smart' objects
“This is a short list of issues you will encounter if you try to use native Android BLE...”