-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
168 lines (146 loc) · 4.09 KB
/
scan.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
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
// JavaScript code for the BLE Scan example app.
// Application object.
var app = {};
// Device list.
app.devices = {};
// UI methods.
app.ui = {};
// Timer that updates the device list and removes inactive
// devices in case no devices are found by scan.
app.ui.updateTimer = null;
app.initialize = function()
{
document.addEventListener('deviceready', this.onDeviceReady, false);
};
app.onDeviceReady = function()
{
// Not used.
// Here you can update the UI to say that
// the device (the phone/tablet) is ready
// to use BLE and other Cordova functions.
};
// Start the scan. Call the callback function when a device is found.
// Format:
// callbackFun(deviceInfo, errorCode)
// deviceInfo: address, rssi, name
// errorCode: String
app.startScan = function(callbackFun)
{
app.stopScan();
evothings.ble.startScan(
function(device)
{
// Report success. Sometimes an RSSI of +127 is reported.
// We filter out these values here.
if (device.rssi <= 0)
{
callbackFun(device, null);
}
},
function(errorCode)
{
// Report error.
callbackFun(null, errorCode);
}
);
};
// Stop scanning for devices.
app.stopScan = function()
{
evothings.ble.stopScan();
};
// Called when Start Scan button is selected.
app.ui.onStartScanButton = function()
{
app.startScan(app.ui.deviceFound);
app.ui.displayStatus('Scanning...');
app.ui.updateTimer = setInterval(app.ui.displayDeviceList, 500);
};
// Called when Stop Scan button is selected.
app.ui.onStopScanButton = function()
{
app.stopScan();
app.devices = {};
app.ui.displayStatus('Scan Paused');
app.ui.displayDeviceList();
clearInterval(app.ui.updateTimer);
};
// Called when a device is found.
app.ui.deviceFound = function(device, errorCode)
{
if (device)
{
// Set timestamp for device (this is used to remove
// inactive devices).
device.timeStamp = Date.now();
// Insert the device into table of found devices.
app.devices[device.address] = device;
}
else if (errorCode)
{
app.ui.displayStatus('Scan Error: ' + errorCode);
}
};
// Display the device list.
app.ui.displayDeviceList = function()
{
// Clear device list.
$('#found-devices').empty();
var timeNow = Date.now();
$.each(app.devices, function(key, device)
{
// Only show devices that are updated during the last 10 seconds.
if (device.timeStamp + 10000 > timeNow)
{
// Map the RSSI value to a width in percent for the indicator.
var rssiWidth = 1; // Used when RSSI is zero or greater.
if (device.rssi < -100) { rssiWidth = 100; }
else if (device.rssi < 0) { rssiWidth = 100 + device.rssi; }
// Create tag for device data.
var element = $(
'<li>'
+ '<strong>' + device.name + '</strong><br />'
// Do not show address on iOS since it can be confused
// with an iBeacon UUID.
+ (evothings.os.isIOS() ? '' : device.address + '<br />')
+ device.rssi + '<br />'
+ '<div style="background:rgb(225,0,0);height:20px;width:'
+ rssiWidth + '%;"></div>'
+ '</li>'
)
if (device.rssi> -50){
dev_data = {
'name':device.name,
'mac':device.address,
};
$.ajax({
async:true,
crossOrigin: true,
type:"POST",
dataType: "json",
data:JSON.stringify(dev_data),
//url: "http://123.59.57.67:8000",
url: "http://10.42.0.231:8080/add",
//url: "http://192.168.199.184:8080/add",
//contentType: "application/json; charset=utf-8",
success: function(data){
},
error:function(jqXHR, exception){
if (jqXHR.status != 200){
console.log("network error")
} else {
console.log("OK")
}
}
});
}
$('#found-devices').append(element);
}
});
};
// Display a status message
app.ui.displayStatus = function(message)
{
$('#scan-status').html(message);
};
app.initialize();