Skip to content

RadiusNetworks/javascript-beacon-library

Repository files navigation

JavaScript Beacon Library

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.

Usage

The JavaScript Beacon Library creates window.beacon of type Beacon

Advertising

To register an advertisement use the registerAdvertisement() method:

AltBeacon

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));

Eddystone UID

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));

Eddystone URL

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));

Stop advertising

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));
});

Register a custom beacon type

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.

Build and test the library

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

API

Modules

beacon-chrome-os
beacon-data
beacon-layout
beacon
platform

Members

beacon : module:javascript-beacon-library

The global beacon instance.

beacon-chrome-os

BeaconChromeOS ⏏

This class wraps the underlying ChromeOS BLE Advertising API.

Kind: Exported class

beacon-data

BeaconData ⏏

This class provides helper functions that relate to constructing beacon data.

Kind: Exported class

BeaconData.constructBeaconData()

Constructs valid beacon manufacturer or service data for different beacon types

Kind: static method of BeaconData

BeaconData.getByteArray(value) ⇒ Array.<number>

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.

beacon-layout

BeaconParser ⏏

This class provides helper functions that relate to deconstructing beacon beacon.

Kind: Exported class

BeaconParser.parseLayout()

Constructs an ordered array of matchers, identifiers, advertised power, and data based on the beacon layout

Kind: static method of BeaconParser

beacon

platform

platform() ⇒ Object

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

beacon : module:javascript-beacon-library

The global beacon instance.

Kind: global variable