@abandonware/noble
Version:
A Node.js BLE (Bluetooth Low Energy) central library.
66 lines (56 loc) • 1.94 kB
JavaScript
const noble = require('../index');
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
noble.startScanning([], false);
} else {
noble.stopScanning();
}
});
noble.on('discover', function (peripheral) {
console.log(`${new Date()}`);
console.log(
`Peripheral discovered (${peripheral.id} with address <${peripheral.address}, ${peripheral.addressType}>, connectable: ${peripheral.connectable}, scannable: ${peripheral.scannable}, RSSI ${peripheral.rssi}:`
);
console.log('\thello my local name is:');
console.log(`\t\t${peripheral.advertisement.localName}`);
console.log(
'\tcan I interest you in any of the following advertised services:'
);
console.log(`\t\t${JSON.stringify(peripheral.advertisement.serviceUuids)}`);
const serviceData = peripheral.advertisement.serviceData;
if (serviceData && serviceData.length) {
console.log('\there is my service data:');
for (const i in serviceData) {
console.log(
`\t\t${JSON.stringify(serviceData[i].uuid)}: ${JSON.stringify(
serviceData[i].data.toString('hex')
)}`
);
}
}
if (peripheral.advertisement.manufacturerData) {
console.log('\there is my manufacturer data:');
console.log(
`\t\t${JSON.stringify(
peripheral.advertisement.manufacturerData.toString('hex')
)}`
);
}
if (peripheral.advertisement.txPowerLevel !== undefined) {
console.log('\tmy TX power level is:');
console.log(`\t\t${peripheral.advertisement.txPowerLevel}`);
}
console.log();
});
process.on('SIGINT', function () {
console.log('Caught interrupt signal');
noble.stopScanning(() => process.exit());
});
process.on('SIGQUIT', function () {
console.log('Caught interrupt signal');
noble.stopScanning(() => process.exit());
});
process.on('SIGTERM', function () {
console.log('Caught interrupt signal');
noble.stopScanning(() => process.exit());
});