UNPKG

bleat

Version:

Abstraction library following Web Bluetooth specification for hiding differences in JavaScript BLE APIs

94 lines (82 loc) 2.63 kB
var bluetooth = require('../index').webbluetooth; var bluetoothDevices = []; function logError(error) { console.log(error); process.exit(); } process.stdin.setEncoding('utf8'); process.stdin.on('readable', () => { var input = process.stdin.read(); if (input === '\u0003') { process.exit(); } else { var index = parseInt(input); if (index && index <= bluetoothDevices.length) { process.stdin.setRawMode(false); selectDevice(index - 1); } } }); function enumerateGatt(server) { return server.getPrimaryServices() .then(services => { var sPromises = services.map(service => { return service.getCharacteristics() .then(characteristics => { var cPromises = characteristics.map(characteristic => { return characteristic.getDescriptors() .then(descriptors => { descriptors = descriptors.map(descriptor => `\t\t└descriptor: ${descriptor.uuid}`); descriptors.unshift(`\t└characteristic: ${characteristic.uuid}`); return descriptors.join("\n"); }); }); return Promise.all(cPromises) .then(descriptors => { descriptors.unshift(`service: ${service.uuid}`); return descriptors.join("\n"); }); }); }); return Promise.all(sPromises) .then(services => { console.log(services.join("\n")); }); }); } function handleDeviceFound(bluetoothDevice, selectFn) { var discovered = bluetoothDevices.some(device => { return (device.id === bluetoothDevice.id); }); if (discovered) return; if (bluetoothDevices.length === 0) { process.stdin.setRawMode(true); console.log("select a device:"); } bluetoothDevices.push({ id: bluetoothDevice.id, select: selectFn }); console.log(bluetoothDevices.length + ": " + bluetoothDevice.name); } function selectDevice(index) { var device = bluetoothDevices[index]; device.select(); } var server = null; console.log("scanning..."); bluetooth.requestDevice({ deviceFound: handleDeviceFound }) .then(device => { console.log("connecting..."); return device.gatt.connect(); }) .then(gattServer => { console.log("connected"); server = gattServer; return enumerateGatt(server); }) .then(() => server.disconnect()) .then(() => { console.log("\ndisconnected"); process.exit(); }) .catch(logError);