Skip to content

Instantly share code, notes, and snippets.

@astagi
Created December 22, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astagi/95a2f0423495249e528493566d7b1096 to your computer and use it in GitHub Desktop.
Save astagi/95a2f0423495249e528493566d7b1096 to your computer and use it in GitHub Desktop.
R2D2
const noble = require('noble');
let connectTheDroid = (address) => {
return new Promise((resolve, reject) => {
noble.on('discover', (peripheral) => {
if (peripheral.address === address) {
noble.stopScanning();
peripheral.connect( (e) => {
peripheral.discoverServices([CONNECT_SERVICE], (error, services) => {
services[0].discoverCharacteristics([CONNECT_CHAR], (error, characteristics) => {
characteristics[0].write(Buffer.from(MSG_CONNECTION), true, (error) => {
peripheral.discoverServices([MAIN_SERVICE], (error, services) => {
services[0].discoverCharacteristics([MAIN_CHAR], (error, characteristics) => {
resolve(characteristics[0]);
});
});
});
});
});
});
}
});
noble.on('stateChange', (state) => {
if (state === 'poweredOn') {
noble.startScanning();
} else {
noble.stopScanning();
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment