Last active
August 29, 2015 14:20
-
-
Save sandeepmistry/ea3e1fbacc795e311fb4 to your computer and use it in GitHub Desktop.
node URI Beacon Scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var UriBeacon = require('./uri-beacon'); | |
UriBeacon.on('discover', function(uriBeacon) { | |
console.log('discovered: ' + JSON.stringify(uriBeacon, null, 2)); | |
}); | |
UriBeacon.startScanning(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var events = require('events'); | |
var util = require('util'); | |
var debug = require('debug')('uri-beacon'); | |
var noble = require('noble'); | |
var SERVICE_UUID = 'fed8'; | |
var PREFIXES = [ | |
'http://www.', | |
'https://www.', | |
'http://', | |
'https://', | |
'urn:uuid:' | |
]; | |
var SUFFIXES = [ | |
'.com/', | |
'.org/', | |
'.edu/', | |
'.net/', | |
'.info/', | |
'.biz/', | |
'.gov/', | |
'.com', | |
'.org', | |
'.edu', | |
'.net', | |
'.info', | |
'.biz', | |
'.gov' | |
]; | |
var UriBeacon = function() { | |
noble.on('discover', this.onDiscover.bind(this)); | |
}; | |
util.inherits(UriBeacon, events.EventEmitter); | |
UriBeacon.prototype.startScanning = function() { | |
debug('startScanning'); | |
var startScanningOnPowerOn = function() { | |
if (noble.state === 'poweredOn') { | |
noble.startScanning([SERVICE_UUID], true); | |
} else { | |
noble.once('stateChange', startScanningOnPowerOn); | |
} | |
}; | |
startScanningOnPowerOn(); | |
}; | |
UriBeacon.prototype.stopScanning = function() { | |
debug('stopScanning'); | |
noble.stopScanning(); | |
}; | |
UriBeacon.prototype.onDiscover = function(peripheral) { | |
debug('onDiscover: %s', peripheral); | |
var serviceData = peripheral.advertisement.serviceData; | |
var rssi = peripheral.rssi; | |
debug('onDiscover: serviceData = %s, rssi = %d', serviceData ? serviceData.toString('hex') : null, rssi); | |
if (serviceData && | |
serviceData.length === 1 && | |
serviceData[0].uuid === SERVICE_UUID) { | |
var uriBeaconData = serviceData[0].data; | |
var flags = uriBeaconData.readUInt8(0); | |
var txPower = uriBeaconData.readInt8(1); | |
var uriData = uriBeaconData.slice(2); | |
var uriPrefix = ''; | |
var uriSuffix = ''; | |
var firstByte = uriData[0]; | |
var lastByte = uriData[uriData.length - 1]; | |
if (firstByte < PREFIXES.length) { | |
uriPrefix = PREFIXES[firstByte]; | |
uriData = uriData.slice(1); | |
} | |
if (lastByte < SUFFIXES.length) { | |
uriSuffix = SUFFIXES[lastByte]; | |
uriData = uriData.slice(0, uriData.length - 1); | |
} | |
var uri = uriPrefix + uriData.toString() + uriSuffix; | |
var uriBeacon = { | |
flags: flags, | |
txPower: txPower, | |
uri: uri, | |
rssi: rssi | |
}; | |
debug('onDiscover: uriBeacon = %s', JSON.stringify(uriBeacon)); | |
this.emit('discover', uriBeacon); | |
} | |
}; | |
module.exports = new UriBeacon(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment