-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (41 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var tessel = require('tessel');
var blelib = require('ble-ble113a');
var encoding = require('eddystone-url-encoding');
var ble = blelib.use(tessel.port['A']);
var readyCB = function(err) {
console.log('Scanning...');
ble.startScanning();
};
ble.on('ready', readyCB);
ble.on('discover', function(peripheral) {
console.log("Discovered peripheral!", peripheral.toString());
var uuid = getUUID(peripheral.advertisingData);
console.log("UUID", uuid.toString());
if (uuid.toString() == 'feaa') {
console.log("Found physical web beacon!");
var url = getUrl(peripheral.advertisingData);
console.log("URL", url.toString());
}
});
function getUUID(data) {
var uuid = "NA";
data.forEach(function(e){
if (e.typeFlag == 3) {
uuid = e.data;
}
});
return uuid;
}
function getUrl(data) {
var url = "NA";
var hex = [];
data.forEach(function(e){
if (e.typeFlag == 22) {
url = e.data;
}
});
url.forEach(function(e) {
hex.push(parseInt(e, 16));
});
return encoding.decode(new Buffer(hex.slice(4)));
}