This is a Cordova plugin for Eddystone Beacons on Android and iOS. Use this plugin to scan for Eddystone beacons from your mobile application.
Evothings Studio is a rapid development tool for mobile IoT apps. It is fully compatible with Apache Cordova and PhoneGap.
With Evothings Studio the edit/run turn-around cycle is just a second or two, which is much faster compared to the traditional method of rebuilding the Cordova project for each update.
Create a Cordova project (replace folder, domain and app name with your own names):
cordova create appfolder com.mydomain.myeddystoneapp Eddystone
Go to the project folder:
cd appfolder
Add the Eddystone plugin:
cordova plugin add cordova-plugin-eddystone
Add others plugins you find useful, such as cordova-plugin-console, which makes console.log direct output to the log in Xcode or adb logcat:
cordova plugin add cordova-plugin-console
Empty the www folder of the Cordova project and drop in a copy of the example index.html file, the result should look like this:
www
index.html
Add platforms (Android and/or iOS):
cordova platform add android
cordova platform add ios
Build:
cordova build
Run the app by deploying it to a mobile device.
For displaying notifications when the app is running in the background, use the plugin de.appplant.cordova.plugin.local-notification:
cordova plugin add de.appplant.cordova.plugin.local-notification
The Eddystone library is built on top of the Cordova BLE plugin. The Eddystone plugin packages the BLE plugin and the required JavaScript libraries into one package for convenience.
It can however be more flexible to use the Eddystone library on top of the BLE plugin, and include the libraries yourself. This is how the Eddystone example apps shipped with Evothings Studio work. This makes it possible to modify the Eddystone code on-the-fly, the library files are not "locked in" to a plugin.
To use Eddystone libraries directly, use Evothings Viewer or build a custom Cordova app and add the the Cordova BLE plugin. Then include libs/evothings/eddystone/eddystone.js
in index.html
, as shown in the Eddystone Scan example app. The actual library files are in the folder libs, copy this folder to the Cordova www
directory.
Generated documentation is found at the Evothings documentation site.
Below follows an overview of the functions and data strutures in the Eddystone library.
Starts scanning for Eddystone devices. Found devices and errors will be reported to the supplied callbacks.
successCallback is a function that is called repeatedly when a device is found. Typically the same device is reported multiple times. The interval will depend of the advertisement interval of the beacon, among other things.
startScan will keep scanning until you call stopScan().
To conserve energy, call stopScan() as soon as you've found the device you're looking for. Calling this function while scanning is in progress will produce an error.
Format:
evothings.eddystone.startScan(successCallback, errorCallback)
Format for successCallback (beacon is an object that represents the found device, see description of EddystoneDevice below):
successCallback({EddystoneDevice} beacon)
Format for errorCallback (error is a string value):
errorCallback({string} error)
Object representing an Eddystone BLE device. Inherits from evothings.easyble.EasyBLEDevice. Note that the Eddystone specific object properties are optional, they may be missing from the object (give value null when accessed). Property values are filled in as different Eddystone frame types broadcasted from the beacon are detected.
The following are properties relevant for Eddystone beacons:
Other properties that may be of interest:
Stops scanning for Eddystone devices.
Example:
evothings.eddystone.stopScan()
To find the approximate distance in meters from the beacon, use function calculateAccuracy:
var distance = evothings.eddystone.calculateAccuracy(
beacon.txPower, beacon.rssi)
Note that beacon.txPower and beacon.rssi many be undefined, in which case calculateAccuracy returns null. This happens before txPower and rssi values have been reported by the beacon.
Here is a quick guide to how to use the Eddystone plugin.
This is how to scan for beacons:
function foundBeacon(beacon)
{
// Note that beacon.url will be null until the URL
// has been received. Also note that not all Eddystone
// beacons broadcast URLs, they may send UIDs only.
console.log('Found beacon: ' + beacon.url)
}
function scanError(error)
{
console.log('Eddystone scan error: ' + error)
}
evothings.eddystone.startScan(foundBeacon, scanError)
Alternative syntax:
evothings.eddystone.startScan(
function(beacon)
{
console.log('Found beacon: ' + beacon.url)
},
function(error)
{
console.log('Eddystone scan error: ' + error)
})
This will log all device properties:
function foundBeacon(beacon)
{
console.log(JSON.stringify(beacon))
}
evothings.eddystone.stopScan()
There is an example app bundled with the plugin, see file index.html in the examples folder.
The Eddystone plugin is implemented on top the the Cordova BLE plugin.
The JavaScript code for the plugin is found in file eddystone-plugin.js. This file is generated by the Ruby script buildEddystonePluginJS.rb.
The JavaScript source files for the plugin are found in the evothings-examples repo. If you would wish to run buildEddystonePluginJS.rb yourself, you need to first clone evothings-libraries next to the cordova-eddystone folder.
Do not contribute code directly to eddystone-plugin.js, but rather to the source files in evothings-libraries.
Explore the world of rapid development of mobile IoT apps! Download Evothings Studio now - it is easy and fun to get started!