-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
206 lines (168 loc) · 5.28 KB
/
Copy pathapp.js
File metadata and controls
206 lines (168 loc) · 5.28 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// This function will build the xapi statement to report beacon detection
// or loss of signal
function xapi_notify(verb, status, key){
var d = new Date();
var n = d.toISOString();
var statement = {
"timestamp": n,
"actor": {
"mbox": "mailto:altierian@gmail.com",
"name": "EMT_002",
"objectType": "Agent"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/" + verb,
"display": {
"en-US": verb
}
},
"object": {
"id": "http://adlnet.gov/expapi/activities/beacon",
"definition": {
"name": {
"en-US": "beacon_tag"
},
"description": {
"en-US": "beacon_tag has been " + status
}
},
"objectType": "Activity"
},
"result": {
"response": key
}
};
// Dispatch the statement to the LRS
ADL.XAPIWrapper.sendStatement(statement);
alert("Statement Submitted");
}
var app = (function()
{
// Application object.
var app = {};
// Specify your beacon 128bit UUIDs here.
var regions =
[
// Estimote Beacon factory UUID.
// Sample UUIDs for beacons in our lab.
{uuid:'2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6'}
];
// Dictionary of beacons.
var beacons = {};
// Timer that displays list of beacons.
var updateTimer = null;
app.initialize = function()
{
document.addEventListener('deviceready', onDeviceReady, false);
};
function onDeviceReady()
{
// Specify a shortcut for the location manager holding the iBeacon functions.
window.locationManager = cordova.plugins.locationManager;
// Start tracking beacons!
startScan();
// Display refresh timer.
updateTimer = setInterval(displayBeaconList, 500);
}
function startScan()
{
// The delegate object holds the iBeacon callback functions
// specified below.
var delegate = new locationManager.Delegate();
// Called continuously when ranging beacons.
delegate.didRangeBeaconsInRegion = function(pluginResult)
{
var count = 0;
//console.log('didRangeBeaconsInRegion: ' + JSON.stringify(pluginResult))
for (var i in pluginResult.beacons)
{
// Insert beacon into table of found beacons.
var beacon = pluginResult.beacons[i];
beacon.timeStamp = Date.now();
var key = beacon.uuid + ':' + beacon.major + ':' + beacon.minor;
// Check to see if we've already found this beacon, if we have, ignore it.
// If not, send the xAPI statement that the beacon was enabled
if (key in beacons) {}
else {xapi_notify("initialized", "enabled", key);}
beacons[key] = beacon;
}
};
// Called when starting to monitor a region.
// (Not used in this example, included as a reference.)
delegate.didStartMonitoringForRegion = function(pluginResult)
{
//console.log('didStartMonitoringForRegion:' + JSON.stringify(pluginResult))
};
// Called when monitoring and the state of a region changes.
// (Not used in this example, included as a reference.)
delegate.didDetermineStateForRegion = function(pluginResult)
{
//console.log('didDetermineStateForRegion: ' + JSON.stringify(pluginResult))
};
// Set the delegate object to use.
locationManager.setDelegate(delegate);
// Request permission from user to access location info.
// This is needed on iOS 8.
locationManager.requestAlwaysAuthorization();
// Start monitoring and ranging beacons.
for (var i in regions)
{
var beaconRegion = new locationManager.BeaconRegion(
i + 1,
regions[i].uuid);
// Start ranging.
locationManager.startRangingBeaconsInRegion(beaconRegion)
.fail(console.error)
.done();
// Start monitoring.
// (Not used in this example, included as a reference.)
locationManager.startMonitoringForRegion(beaconRegion)
.fail(console.error)
.done();
}
}
function displayBeaconList()
{
// Clear beacon list.
$('#found-beacons').empty();
var timeNow = Date.now();
// Update beacon list.
$.each(beacons, function(key, beacon)
{
// Only show beacons that are updated during the last 60 seconds.
// if (beacon.timeStamp + 60000 > timeNow)
if (beacon.timeStamp + 5000 > timeNow)
{
// Map the RSSI value to a width in percent for the indicator.
var rssiWidth = 1; // Used when RSSI is zero or greater.
if (beacon.rssi < -100) { rssiWidth = 100; }
else if (beacon.rssi < 0) { rssiWidth = 100 + beacon.rssi; }
// Create tag to display beacon data.
var element = $(
'<li>'
+ '<strong>UUID: ' + beacon.uuid + '</strong><br />'
+ 'Major: ' + beacon.major + '<br />'
+ 'Minor: ' + beacon.minor + '<br />'
+ 'Proximity: ' + beacon.proximity + '<br />'
+ 'RSSI: ' + beacon.rssi + '<br />'
+ '<div style="background:rgb(255,128,64);height:20px;width:'
+ rssiWidth + '%;"></div>'
+ '</li>'
);
$('#warning').remove();
$('#found-beacons').append(element);
}
else
{
// If the beacon hasn't been heard from in x seconds, send the statement to the LRS
// that the tag was disabled.
xapi_notify("terminated", "disabled", key);
// remove the beacon from the list of found beacons. This way, it will only report
// that it's was disabled once. Also, it will report again the next time it's found.
delete beacons[key];
}
});
}
return app;
})();
app.initialize();