Skip to content

Commit

Permalink
chore(example): peripheral explorer async updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Dec 29, 2024
1 parent d0e2c6d commit 9b4580f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions examples/peripheral-explorer-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ const noble = require('../');

const directConnect = process.argv[2].toLowerCase();
const peripheralIdOrAddress = process.argv[3].toLowerCase();
const addressType = process.argv[4].toLowerCase() || 'random';

const starTime = Date.now();

noble.on('stateChange', async (state) => {
if (state === 'poweredOn') {
async function main () {
try {
await noble.waitForPoweredOn();
if (directConnect === '1') {
await noble.stopScanningAsync();
await noble.connectAsync(peripheralIdOrAddress.replace(/:/g, ''));
const peripheral = await noble.connectAsync(peripheralIdOrAddress.replace(/:/g, ''), { addressType });
await explore(peripheral);
} else {
await noble.startScanningAsync();
}
} catch (error) {
console.error('Error:', error);
}
});
}

noble.on('discover', async (peripheral) => {
if ([peripheral.id, peripheral.address].includes(peripheralIdOrAddress)) {
Expand Down Expand Up @@ -143,3 +147,5 @@ process.on('SIGTERM', function () {
console.log('Caught interrupt signal');
noble.stopScanning(() => process.exit());
});

main();

0 comments on commit 9b4580f

Please sign in to comment.