Skip to content

Instantly share code, notes, and snippets.

@adamplabarge
Last active May 28, 2022 04:47
Show Gist options
  • Save adamplabarge/0b97fd4a7898e74662f875862575c634 to your computer and use it in GitHub Desktop.
Save adamplabarge/0b97fd4a7898e74662f875862575c634 to your computer and use it in GitHub Desktop.
[DOES NOT CURRENTLY SEEM TO WORK] A script to listen to BC08 Water-Resistant iBeacon BLE 4.0/5.0 MultiBeacon
// NOTE - This code does not seem to show motion events from the beacon.
/**
...dependencies & engine
"dependencies": {
"@abandonware/bluetooth-hci-socket": "^0.5.3-8",
"@abandonware/noble": "^1.9.2-15",
"node-beacon-scanner": "^0.2.2"
},
"engines": {
"node": "12.22.12",
"npm": "6.14.16"
}
Buidling on
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)"
NAME="Raspbian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
*/
/**
Current program output.
:START
Scanning...
Discovering... dd340206bd88
Stopping scanning...
dd340206bd88 has advertised as BlueCharm_176227.
{"id":"dd340206bd88","address":"dd:34:02:06:bd:88","localName":"BlueCharm_176227","txPowerLevel":null,"rssi":-55,"beaconType":"iBeacon","iBeacon":{"uuid":"426C7565-4368-6172-6D42-6561636F6E78","major":3838,"minor":4949,"txPower":-59}} null
Scanning stopped.
Connection to peripheral dd340206bd88...
Connected to dd340206bd88
Discovering characteristics...
Characteristics discovered.
Processing characteristics
- Characteristic Device Name
- Characteristic Appearance
- Characteristic Peripheral Preferred Connection Parameters
- Characteristic Central Address Resolution
- Characteristic Manufacturer Name String
- Characteristic Model Number String
- Characteristic Hardware Revision String
- Characteristic Firmware Revision String
- Characteristic System ID
- Characteristic Service Changed
- Characteristic Battery Level
- Characteristic null
- Characteristic null
- Characteristic null
- Characteristic null
:END
And then just does nothing else.
*/
const noble = require('@abandonware/noble')
const BeaconScanner = require('node-beacon-scanner')
const scanner = new BeaconScanner()
const beaconId = 'dd340206bd88'
scanner.onadvertisement = (advertisement) => {
if (advertisement.id === beaconId) {
console.log(`${beaconId} has advertised as ${advertisement.localName}.`)
console.log(JSON.stringify(advertisement.iBeacon), null, ' ')
}
}
scanner.startScan().then(() => {
console.log("Scanning...")
}).catch(err => console.error(err))
noble.on('discover', async (peripheral) => {
console.log(`Discovering... ${peripheral.id}`)
if (peripheral.id === beaconId) {
console.log('Stopping scanning...')
scanner.stopScan()
await noble.stopScanningAsync();
console.log('Scanning stopped.')
console.log(`Connection to peripheral ${peripheral.id}...`)
await peripheral.connectAsync();
console.log(`Connected to ${peripheral.id}`);
console.log('Discovering characteristics...')
const { characteristics } = await peripheral.discoverAllServicesAndCharacteristicsAsync()
console.log('Characteristics discovered.')
console.log('Processing characteristics')
characteristics.map(characteristic => {
console.log(`- Characteristic ${characteristic.name}`)
characteristic.on('data', (data, isNotification) => {
console.log(`Received: "${data}"`);
});
characteristic.subscribe((error) => {
if (error) {
console.error('Error subscribing to echoCharacteristic');
} else {
console.log('Subscribed for echoCharacteristic notifications');
}
});
})
}
//await peripheral.disconnectAsync(() => console.log('disconnected'))
});
process.on('SIGINT', function () {
console.log('Caught interrupt signal');
scanner.stopScan(() => process.exit())
noble.stopScanning(() => process.exit());
process.exit()
});
process.on('SIGQUIT', function () {
console.log('Caught interrupt signal');
scanner.stopScan(() => process.exit())
noble.stopScanning(() => process.exit());
process.exit()
});
process.on('SIGTERM', function () {
console.log('Caught interrupt signal');
scanner.stopScan(() => process.exit())
noble.stopScanning(() => process.exit());
process.exit()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment