forked from noble/noble
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(noble): add direct device connection capability without prior sc…
…anning
- Loading branch information
1 parent
e3cd3e4
commit 77c0210
Showing
8 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const noble = require('../index'); | ||
const direct = require('debug')('connection/direct'); | ||
const scan = require('debug')('connection/scan'); | ||
|
||
function sleep (ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
async function run () { | ||
noble.on('stateChange', async function (state) { | ||
if (state === 'poweredOn') { | ||
try { | ||
direct('connecting'); | ||
// const uuid = 'f1:36:1c:ab:94:cc'.split(':').join(''); // HCI Address UUID | ||
const uuid = '2561b846d6f83ee27580bca8ed6ec079'; // MacOS UUID | ||
const peripheral = await noble.connectAsync(uuid); | ||
direct(`connected ${peripheral.uuid}`); | ||
await peripheral.disconnectAsync(); | ||
direct('disconnected'); | ||
console.log('sleeping for 2000ms'); | ||
await sleep(2000); | ||
scan('connecting by scan'); | ||
await noble.startScanningAsync(); | ||
noble.on('discover', async peripheral => { | ||
if (peripheral.uuid === uuid) { | ||
await noble.stopScanningAsync(); | ||
await peripheral.connectAsync(); | ||
scan(`connected ${peripheral.uuid}`); | ||
await peripheral.disconnectAsync(); | ||
scan('disconnected'); | ||
} | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
run(); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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