@ansgomez/node-beacon-scanner
v0.2.5
Published
The node-beacon-scanner is a Node.js module which allows you to scan BLE beacon packets and parse the packet data. This module supports MiroCards, iBeacon, Eddystone, and Estimote.
Downloads
26
Maintainers
Readme
node-beacon-scanner
The node-beacon-scanner is a Node.js module which allows you to scan BLE beacon packets and parse the packet data. This module supports MiroCard, iBeacon, Eddystone, and Estimote.
The supported beacon data formats are as follows:
- MiroCard
- iBeacon
- Eddystone-UID
- Eddystone-URL
- Eddystone-TLM (Unencrypted)
- Eddystone-EID
- Estimote-Telemetry
- Estimote-Nearable
Dependencies
See the document of the @abandonware/noble for details on installing the @abandonware/noble.
Note that the noble has to be run as root on most of Linux environments. See the the document of the @abandonware/noble for details.
The early versions of this module depended on noble for BLE handling. But the noble seems not to support Node v10 or later versions. Now, this module is employing @abandonware/noble, which was forked from noble. For the purouse of the backward compatibility, this module works with noble on Node v8 or earlier versions.
Installation
$ mkdir -p ~/mirocard-scanner
$ cd ~/mirocard-scanner
$ npm install @abandonware/noble
$ npm install @ansgomez/node-beacon-scanner
Table of Contents
Quick Start
Create a new file inside the mirocard-scanner
folder and paste the following code.
This sample code shows how to start scanning and how to get parsed packets.
const BeaconScanner = require('@ansgomez/node-beacon-scanner');
const scanner = new BeaconScanner();
// Set an Event handler for becons
scanner.onadvertisement = (ad) => {
// Print full payload
// console.log(JSON.stringify(ad, null, ' '));
// console.log(ad.mirocard.ad.manufacturerData);
// Print MiroCard Sensor Data
console.log("MAC: "+ad.address);
console.log("Temperature: "+ad.mirocard.temp);
console.log("Humidity: "+ad.mirocard.rh + "\n");
};
// Start scanning
scanner.startScan().then(() => {
console.log('Started to scan.') ;
}).catch((error) => {
console.error(error);
});
The sample code above will output the result as follows:
Started to scan.
MAC: 60:77:71:57:17:83
Temperature: 23.85
Humidity: 25.60
...
BeaconScanner
object
In order to use the node-beacon-scanner, you have to load the node-beacon-scanner module as follows:
const BeaconScanner = require('node-beacon-scanner');
You can get an BeaconScanner
constructor from the code above. Then you have to create an BeaconScanner
object from the BeaconScanner
constructor as follows:
const scanner = new BeaconScanner();
The BeaconScanner
constructor takes an argument optionally. It must be a hash object containing the properties as follows:
Property | Type | Required | Description
:--------|:-------|:---------|:-----------
noble
| Noble | option | a Noble object of the noble
module
The node-beacon-scanner module uses the noble
module in order to scan the beacons coming from the BLE beacon device(s). If you want to interact other BLE devices using the noble module, you can create an Noble
object by yourself, then pass it to this module. If you don't specify a Noble
object to the noble
property, this module automatically create a Noble
object internally.
The sample code below shows how to pass a Nobel
object to the Linking
constructor.
// Create a Noble object
const noble = require('noble');
// Create a Linking object
const BeaconScanner = require('node-beacon-scanner');
const scanner = new BeaconScanner({'noble': noble});
In the code snippet above, the variable scanner
is a BeaconScanner
object. The BeaconScanner
object has methods as described in sections below.
scartScan() method
The startScan()
method starts to scan advertising packets from BLE beacon devices. This method returns a Promise
object.
Whenever a packet is received, the callback function set to the onadvertisement
property of the BeaconScanner
object will be called. When a packet is received, an BeaconScannerAdvertisement
object will be passed to the callback function.
// Set an Event handler for becons
scanner.onadvertisement = (ad) => {
console.log(JSON.stringify(ad, null, ' '));
};
// Start scanning
scanner.startScan().then(() => {
console.log('Started to scan.') ;
}).catch((error) => {
console.error(error);
});
stopScan() method
The stopScan()
method stops to scan advertising packets from BLE beacon devices. See the section "startScan()
method" for details.
onadvertisement
event handler
If a callback function is set to the onadvertisement
property, the callback function will be called whenever an advertising packet is received from a BLE beacon device during the scan is active (from the moment when the startScan()
method is called, to the moment when the stopScan()
method is called).
See the section "startScan()
method" for details.
BeaconScannerAdvertisement
object
The BeaconScannerAdvertisement
object represents an advertising data coming from the BLE beacon device. This object is just a hash object containing properties as follows. Note that the structure varies depending on the format of the beacon. You can know the format of the beacon from the value of the beaconType
property.
iBeacon
{
"id": "c7dfbfd9f64a",
"address": "c7:df:bf:d9:f6:4a",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "iBeacon",
"iBeacon": {
"uuid": "B9407F30-F5F8-466E-AFF9-25556B57FE6D",
"major": 21983,
"minor": 57807,
"txPower": 180
}
}
The value of the iBeacon
property contains the properties as follows:
Property |Type |Description
:---------|:-------|:----------
uuid
| String | Proximity UUID
major
| Number | Major
minor
| Number | Minor
txPower
| Number | Measured Power (dBm)
Eddystone-UID
{
"id": "c6debed8f549",
"address": "c6:de:be:d8:f5:49",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "eddystoneUid",
"eddystoneUid": {
"txPower": -35,
"namespace": "EDD1EBEAC04E5DEFA017",
"instance": "2D3EA3203B6B"
}
}
The value of the eddystoneUid
property contains the properties as follows:
Property |Type |Description
:------------|:-------|:----------
namespace
| String | Namespace ID
instance
| String | Instance ID
txPower
| Number | Calibrated Tx power (dBm)
Eddystone-URL
{
"id": "c6debed8f549",
"address": "c6:de:be:d8:f5:49",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "eddystoneUrl",
"eddystoneUrl": {
"txPower": -35,
"url": "http://go.esti.be/"
}
}
The value of the eddystoneUrl
property contains the properties as follows:
Property |Type |Description
:---------|:-------|:----------
url
| String | Decoded URL
txPower
| Number | Calibrated Tx power (dBm)
Eddystone-TLM (Unencrypted)
{
"id": "c6debed8f549",
"address": "c6:de:be:d8:f5:49",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "eddystoneTlm",
"eddystoneTlm": {
"batteryVoltage": 6059,
"temperature": 28.59765625,
"advCnt": 83,
"secCnt": 361299
}
}
The value of the eddystoneTlm
property contains the properties as follows:
Property |Type |Description
:----------------|:-------|:----------
batteryVoltage
| Number | Battery voltage (mV)
temperature
| Number | Beacon temperature (°C)
advCnt
| Number | Advertising PDU count
secCnt
| Number | Time since power-on or reboot
Eddystone-EID
{
"id": "d9d9fe86fb61",
"address": "d9:d9:fe:86:fb:61",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "eddystoneEid",
"eddystoneEid": {
"txPower": -35,
"eid": "79c58b83f198ddbe"
}
}
The value of the eddystoneEid
property contains the properties as follows:
Property |Type |Description
:---------|:-------|:----------
eid
| String | Ephemeral Identifier
txPower
| Number | Calibrated Tx power (dBm)
Estimote-Telemetry
There are two types in the Estimote Telemetry beacon data: Type A
and Type B
. The types can be obtained checking the value of the subFrameType
property. If the type is A
, the value is 0
. If the type is B
, the value is 1
.
Type A
{
"id": "c9e1c1dbf84c",
"address": "c9:e1:c1:db:f8:4c",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "estimoteTelemetry",
"estimoteTelemetry": {
"frameType": 2,
"subFrameType": 0,
"protocolVersion": 2,
"shortIdentifier": "2d3ea3203b6b2446",
"acceleration": {
"x": -0.015748031496062992,
"y": -0.07874015748031496,
"z": 0.9921259842519685
},
"moving": false,
"gpio": {
"pin0": "low",
"pin1": "low",
"pin2": "high",
"pin3": "high"
},
"errors": {
"firmware": false,
"clock": false
},
"pressure": 100304.9765625
}
}
Type B
{
"id": "c9e1c1dbf84c",
"address": "c9:e1:c1:db:f8:4c",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "estimoteTelemetry",
"estimoteTelemetry": {
"frameType": 2,
"subFrameType": 1,
"protocolVersion": 2,
"shortIdentifier": "2d3ea3203b6b2446",
"magneticField": {
"x": 0,
"y": 0,
"z": 0
},
"light": 80.64,
"uptime": {
"unitCode": 1,
"unitDesc": "minutes",
"value": 596
},
"temperature": 28.75,
"batteryVoltage": 6059,
"batteryLevel": 95
}
}
Estimote-Nearable
{
"id": "f8d3c05e8001",
"address": "f8:d3:c0:5e:80:01",
"localName": null,
"txPowerLevel": null,
"rssi": -59,
"beaconType": "estimoteNearable",
"estimoteNearable": {
"nearableId": "bd20de5cd074de8d",
"temperature": 25.75,
"moving": false,
"acceleration": {
"x": -15.625,
"y": -78.125,
"z": -1031.25
}
}
}
The value of the estimoteNearable
property contains the properties as follows:
Property | |Type |Description
:--------------|:----|:--------|:----------
nearableId
| | String | Nearable identifier
temperature
| | Number | Temperature (°C)
moving
| | Boolean | Moving state (true: moving)
acceleration
| | Object | Acceleration
-- | x
| Number | Acceleration on the X axis (milli G)
-- | y
| Number | Acceleration on the Y axis (milli G)
-- | z
| Number | Acceleration on the Z axis (milli G)
Release Note
- v0.2.3 (2021-03-22)
- Added new MiroCard payload parser
- v0.2.2 (2019-11-24)
- Fixed a bug that the
txPower
of iBeacon was wrong. (Thanks to @girtgirt) - Fixed a bug that an exception was thrown whenever it received an advertisement packet without service data. (Thanks to @charlesread)
- Fixed a bug that the
- v0.2.1 (2019-11-02)
- Fixed a typo of a property name in the
BeaconScannerAdvertisement
object for Eddystone (namespece
->namespace
). (Thanks to @natcl)
- Fixed a typo of a property name in the
- v0.2.0 (2019-10-25)
- Supported Node v10 or later versions thanks to @abandonware/noble
- v0.1.1 (2018-12-27)
- Fixed the bug that Eddystone beacons were not discovered if iBeacons were present as well. (Thanks to @Tiggu)
- v0.1.0 (2018-07-14)
- Supported the Eddystone-UID of Kontakt. The spec of the Eddystone-UID defines that the packet size is 20 bytes (the last 2 bytes are RFU). But the size of the packet form Kontakt device is 18 bytes. (Thanks to @EwaRvr)
- v0.0.3 (2018-06-24)
- Fixed a bug that an exception was thrown when an unknown packet came.
- v0.0.2 (2017-09-15)
- Fixed a bug that an exception was thrown when an unknown packet came.
- v0.0.1 (2017-09-06)
- First public release
References
License
The MIT License (MIT)
Copyright (c) 2017-2019 Futomi Hatano Copyright (c) 2021 Andres Gomez
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.