A JavaScript library for broadcasting BLE Beacon advertisements, wrapping existing
advertising APIs to make beacon transmission easier for developers. The library
is flexible, supporting custom beacon types that can be registered with a beaconLayout
similar to the Android Beacon Library
and ScanBeacon Ruby Gem. The
following beacon types are also supported by default:
NOTE Currently only the Chrome OS platform is supported.
The JavaScript Beacon Library creates window.beacon
of type
Beacon
To register an advertisement use the registerAdvertisement()
method:
let registered_adv;
beacon.registerAdvertisement({
type: 'altbeacon',
ids: ['2F234454CF6D4A0FADF2F4911BA9FFA6', 1, 1],
advertisedTxPower: -59
}).then(advertisement => {
registered_adv = advertisement;
console.log('Advertising: ' + advertisement)
}).catch(error => console.log(error.message));
let registered_adv;
beacon.registerAdvertisement({
type: 'eddystone_uid',
ids: ['2F234454F4911BA9FFA6', '000000000001'],
advertisedTxPower: -20
}).then(advertisement => {
registered_adv = advertisement;
console.log('Advertising: ' + advertisement)
}).catch(error => console.log(error.message));
let registered_adv;
beacon.registerAdvertisement({
type: 'eddystone_url',
ids: ['http://example.com'],
advertisedTxPower: -20
}).then(advertisement => {
registered_adv = advertisement;
console.log('Advertising: ' + advertisement)
}).catch(error => console.log(error.message));
To stop advertising use the unregisterAdvertisement()
method:
registered_adv.unregisterAdvertisement().then(() => {
console.log('Advertisement unregistered successfully.');
}).catch(error => console.log(error.message));
Or if you have multiple advertisements:
beacon.advertisements.forEach(advertisement => {
advertisement.unregisterAdvertisement()
.then(() => console.log('Unregistered successfully'))
.catch(error => console.log('Couldn\'t unregister the advertisement: ' + error.message));
});
To register a custom beacon type use the registerBeaconType()
method:
beacon.registerBeaconType({
type: 'cool_beacon',
beaconLayout: 'm:2-3=0000,i:4-19,i:20-21,i:22-23,p:24-24',
manufacturerId: 0x1234
})
For more information on the beaconLayout
check out
this page
from the Android Beacon Library docs.
Run browser/unit tests with npm:
$ npm run test
Build library with npm:
$ npm run browserify
After building the product can be found under the project root directory
- beacon :
module:javascript-beacon-library
The global beacon instance.
This class wraps the underlying ChromeOS BLE Advertising API.
- beacon-data
- BeaconData ⏏
- .constructBeaconData()
- .getByteArray(value) ⇒
Array.<number>
- BeaconData ⏏
This class provides helper functions that relate to constructing beacon data.
Constructs valid beacon manufacturer or service data for different beacon types
Kind: static method of BeaconData
Validates the give array of bytes or converts the hex string into an array of bytes.
Kind: static method of BeaconData
Returns: Array.<number>
- Array of bytes.
Throws:
TypeError
If |value| is not an array or a string.Error
If |value| contains out-of-range numbers or characters.
Param | Type | Description |
---|---|---|
value | Array.<number> | string |
The value to encode. |
This class provides helper functions that relate to deconstructing beacon beacon.
Constructs an ordered array of matchers, identifiers, advertised power, and data based on the beacon layout
Kind: static method of BeaconParser
Detects what API is available in the platform.
Kind: Exported function
Returns: Object
- An object that wraps the underlying BLE
Advertising API
Throws:
Error
If the platform is unsupported
The global beacon instance.
Kind: global variable