• Stars
    star
    112
  • Rank 306,046 (Top 7 %)
  • Language
    JavaScript
  • Created over 10 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

tracking iOS bluetooth ibeacons with node

Tracking mobile BlueTooth LE iBeacon engagements with Node.js

###iBeacons and Bluetooth Low Energy (BLE) Bluetooth Low Energy (BLE) beacons have come into the world around us with Apple’s branded iBeacon which gives branded convention and integration of the BLE devices. iBeacons break down BLE into 3 core features: broadcast, monitoring and ranging.

tab 1

iBeacon is similar in some ways to mobile GPS Geolocation services. Unlike GPS Geolocation, which gives mobile apps vicinity awareness (limited to outdoor scenarios that have line of sight to GPS satellites), BLE iBeacon gives mobile application’s proximity awareness from other iBeacon devices. The distance can be used to triangulate where the user is from a known fixed beacon location or identify if the user is within a predefined nominal “immediate”, “near” or “far” proximity to a device.

tab 1

###Bluetooth proliferation The branding and assimilation of BLE into Apples iOS7 platform is causing a proliferation of Bluetooth ‘beacon-ing’ technology. Large implementations of the technology such as MLB's installation into ball parks , and PayPal's Beacon payment system are making facilities and locations smart. Startups like Estimote, Shelf Bucks, Pebble are making it easier for developers and businesses to integrate the technology into their customers’ daily lives and their mobile app’s. The technology is poised to forever change kiosks, retail end-caps, payment terminals and the facilities around us. Historically these engagement touch points were blind, dumb and mute to your mobile device. Historically the restrooms in the bathroom were the smartest machine in your average retail facility; at least the toilet knew to flush when a person stepped away. iBeacons will make stores, and the devices inside them aware of the mobile devices (more importantly user) in the facility. This gives analytics and insight into the previously dark world of “in store” behavior and engagement changing everything in retail shopping experiences.

###You are your mobile ‘signature' When you open a BLE enabled mobile app your Blue Tooth ‘signature' tracks your location and behavior by your proximity to other BLE aware machines and devices. In a similar way to how a web cookie allows retailers to track your journey through a web site, your real world engagements can be tracked as you interact with BLE aware devices.

###The Power of Node.js Using StrongLoops LoopBack and the open source bleacon npm package it’s very easy to track an iBeacon enabled mobile app using Node.js. Will show you how to do this on your local machine and track the engagements from the machine interaction for later analysis and reporting.

###Tracking BLE engagements with 6 commands and 25 LOC You can build the demo on your own and see it run on a mac book pro (requires Mavericks) with Node.js by simply cloning the repo ‘git clone https://github.com/mschmulen/tracking-bluetooth-ibeacons-with-node’ and running it with ‘slc run app.js’ or build it from scratch by following the instructions below:

Commands:

npm install -g strong-cli
slc lb project ibeacon-node-collector
cd ibeacon-node-collector
npm install --save bleacon
slc lb model ibeacon
slc lb model engagement

The commands above installs the strong-climaking giving us access to the slc lb commands to quickly scaffold an API tier and add our beacon and engagement data models. Installs the bleacon npm package and dependencies.

Code:

Create a folder /boot under the ibeacon-node-collector folder and a file init.js . Add the 10 lines of code below to the newly created ~/ibeacon-node-collector/boot/init.js file.

var ibeacon = app.models.ibeacon;
var Bleacon = require('bleacon');
var ibeaconCache = new Array();

Bleacon.on('discover', function(bleacon) {
	console.log( bleacon );
	
	var myID = 0;
	var guid = bleacon.uuid + bleacon.major + bleacon.minor;
	console.log ( guid + ":" + bleacon.uuid + "," + bleacon.major + "," +  bleacon.minor );
	
	if ( guid in ibeaconCache ) { myID = ibeaconCache[guid]; }
	
	ibeacon.upsert(
			{ id: myID , guid:guid, uuid:bleacon.uuid, major:bleacon.major, minor:bleacon.minor},
			function( error, _ibeacon) 
			{
				ibeaconCache[guid] = _ibeacon.id;
				if ( error ) { console.log( "error on upsert " ); }
				else {
					console.log( "upsert - :" + _ibeacon.id  +  " guid " + _ibeacon.guid );
					var eng = app.models.engagement;
					eng.create({ pBeaconID: _ibeacon.id, timestamp: Date.now(), rssi: bleacon.rssi, proximity: bleacon.proximity });
				} //end else success
		}); //end upser
	
	var signal = [bleacon.rssi/5];
  var xs = new Array(Math.floor(-bleacon.rssi/2) + 1).join('x');
  console.log("rssi:" +xs);
	
});//end on discover

Bleacon.startScanning();

Test the implementation by installing an app that has iBeacon support. I'm using the Estimote Virtual Beacon on the App Store for this demo.

Start the Estimote App in 'iBeacon broadcasting' mode.

tab 1

Run the node application on your host machine with

slc run app.js

In addition to entering the engagement in the in memory data store the code above will shows the BLE Received signal strength indication (RSSI) to the console log as your Node application receives Bleacon discover events. The RSSI value is used to determine the proximity (distance) of a BLE peripheral.

tab 1

###Run your node app anywhere and persist the data to MongoDB

Since the app is using Node can run the collector anywhere that supports the Node.js runtime, such as on a Raspberry-Pi. Allowing you to to create a simple “sniffer” device that when plugged into the wall will track all the Bluetooth engagements that occur in your home, office or store.

If you want to persist your analytics data you can simply change the LoopBack model binding in datasources.json to point to supported data store such as MongoDB, MySQL, or Oracle. you can find more information on configuring your Node.js API to work with LoopBack supported data stores and connectors in the StrongLoop documentation.

Lets look at some of the beacon signatures and engagements that we have captured using the LoopBack Explorer feature on our local machine by opening http://localhost:3000/explorer/ in our web browser.

tab 1

###Search and filter API for engagement analysis

Using the LoopBack API you can get all the beacon signatures in the system from JSON API endpoint at http://localhost:3000/api/engagements.

tab 1

We can also filter engagements based on a GUID using the built in [filter functionality] (http://localhost:3000/api/engagements?filter[where][cBeaconGUID]=87209302c7f24d56b1d114eadd0ce41f15) with the following url request http://localhost:3000/api/engagements?filter[where][id]=1 .

We can also do complex filtering against engagement [time] (http://localhost:3000/api/engagements?filter[where][id]=87209302c7f24d56b1d114eadd0ce41f15&filter[where][time][gt]=87209302c7f24d56b1d114eadd0ce41f16), [proximity] (http://localhost:3000/api/engagements?filter[where][proximity]=near ) or any mix of the available parameters.

http://localhost:3000/api/engagements?filter[where][id]=2&filter[where][proximity]=near

tab 1

###Identify anonymous mobile beacon signatures and track people

Match the user to the anonymous signature and start tracking people within 100 feet of our machine.

If you want to go the extra mile, simply use the built in LoopBack user model object by adding an index reference to the beacon model. When you convert all the anonymous beacon signatures to known users the engagements analytics show real world user behavior.

The conversion from an anonymous unknown user BLE iBeacon signature to known user behavior is the reason that retailers want you to open their customer loyalty app in the store and login. Binding your user profiler to the device and allowing them to track your in store loitering and behavior (and possibly target engagements at you) in the same way they track your shopping behaviors online.