Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a bluetooth debug script that list detected devices #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"lint": "eslint src/**.ts --max-warnings=0",
"watch": "npm run build && npm link && nodemon",
"build": "rimraf ./dist && tsc",
"prepublishOnly": "npm run lint && npm run build"
"prepublishOnly": "npm run lint && npm run build",
"debugBluetooth": "node scripts/debug_bluetooth.js"
},
"keywords": [
"homebridge-plugin"
Expand Down
55 changes: 55 additions & 0 deletions scripts/debug_bluetooth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

const noble = require('@abandonware/noble');

noble.on('stateChange', async (state) => {
console.log(state);
if (state === 'poweredOn') {
console.log('Starting to scan for 30s...');
setTimeout(async () => {
console.log('Stop scanning');
await noble.stopScanningAsync();
process.exit(0);
}, 30_000);
await noble.startScanningAsync([], false);
}
});


const decoder = new TextDecoder('utf-8');
noble.on('discover', async (peripheral) => {
console.log(`Found periphal ${peripheral.id} at address ${peripheral.address}`);

if (peripheral.state === 'disconnected') {
await peripheral.connectAsync();
}

try {
const { characteristics } = await peripheral.discoverSomeServicesAndCharacteristicsAsync(['180a'], ['2a29', '2a24']);

if (characteristics.length === 0) {
return;
}

const device = {};


await Promise.all(characteristics.map(async c => {
const d = await c.readAsync();
const value = decoder.decode(d);
switch (c.uuid) {
case '2a29':
device.manufacturer = value;
break;
case '2a24':
device.model = value;
}
}));

console.log(`Connected to periphal ${peripheral.id} at address ${peripheral.address} (${device.manufacturer} ${device.model}, ${peripheral.advertisement.localName})`);

} catch(e) {

}

await peripheral.disconnectAsync();
});