From 9b4580f666f64aa3e810b958824cc03a73c69a29 Mon Sep 17 00:00:00 2001 From: Marek Serafin Date: Sun, 29 Dec 2024 13:36:50 +0100 Subject: [PATCH] chore(example): peripheral explorer async updates --- examples/peripheral-explorer-async.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/peripheral-explorer-async.js b/examples/peripheral-explorer-async.js index 27e3f6c8..ecd94468 100644 --- a/examples/peripheral-explorer-async.js +++ b/examples/peripheral-explorer-async.js @@ -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)) { @@ -143,3 +147,5 @@ process.on('SIGTERM', function () { console.log('Caught interrupt signal'); noble.stopScanning(() => process.exit()); }); + +main();