Created
December 22, 2019 21:47
-
-
Save astagi/95a2f0423495249e528493566d7b1096 to your computer and use it in GitHub Desktop.
R2D2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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