-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
176 lines (155 loc) · 5.61 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
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
var THERMOMETER_SERVICE = 'BBB0';
var TEMPERATURE_CHARACTERISTIC = 'BBB1';
// i must be < 256
function asHexString(i) {
var hex;
hex = i.toString(16);
// zero padding
if (hex.length === 1) {
hex = "0" + hex;
}
return "0x" + hex;
}
function parseAdvertisingData(buffer) {
var length, type, data, i = 0, advertisementData = {};
var bytes = new Uint8Array(buffer);
while (length !== 0) {
length = bytes[i] & 0xFF;
i++;
// decode type constants from https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
type = bytes[i] & 0xFF;
i++;
data = bytes.slice(i, i + length - 1).buffer; // length includes type byte, but not length byte
i += length - 2; // move to end of data
i++;
advertisementData[asHexString(type)] = data;
}
return advertisementData;
}
var app = {
initialize: function() {
this.bindEvents();
this.showMainPage();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('backbutton', this.onBackButton, false);
deviceList.addEventListener('click', this.connect, false);
refreshButton.addEventListener('click', this.refreshDeviceList, false);
disconnectButton.addEventListener('click', this.disconnect, false);
},
onDeviceReady: function() {
FastClick.attach(document.body); // https://github.com/ftlabs/fastclick
app.refreshDeviceList();
},
refreshDeviceList: function() {
deviceList.innerHTML = ''; // empty the list
ble.scan([THERMOMETER_SERVICE], 10, app.onDiscoverDevice, app.onError);
refreshButton.hidden = true;
scanStatusDiv.innerHTML = "Scanning...";
setTimeout(function() {
refreshButton.hidden = false;
scanStatusDiv.innerHTML = "";
},10000);
},
onDiscoverDevice: function(device) {
var temperature = app.parseTemperature(device);
var listItem = document.querySelector('[data-device-id="' + device.id + '"]');
if (!listItem) { // need to create list item
listItem = document.createElement('li');
deviceList.appendChild(listItem);
}
listItem.innerHTML = device.name + '<br/>' +
device.id + '<br/>' +
'RSSI: ' + device.rssi +
temperature;
listItem.dataset.deviceId = device.id;
},
parseTemperature: function(device) {
// get the temperature from service data in the advertising packet
var celsius, fahrenheit, serviceData;
var temperature = '';
if (cordova.platformId === 'ios') {
serviceData = device.advertising.kCBAdvDataServiceData;
if (serviceData && serviceData.BBB0) {
celsius = new Float32Array(serviceData.BBB0)[0];
}
} else { // android
var SERVICE_DATA_KEY = '0x16';
var advertisingData = parseAdvertisingData(device.advertising);
serviceData = advertisingData[SERVICE_DATA_KEY];
if (serviceData) {
// first 2 bytes are the 16 bit UUID
var uuidBytes = new Uint16Array(serviceData.slice(0,2));
var uuid = uuidBytes[0].toString(16); // hex string
console.log("Found service data for " + uuid);
// remaining bytes are the service data, expecting 32bit floating point number
var data = new Float32Array(serviceData.slice(2));
celsius = data[0];
}
}
if (celsius) {
fahrenheit = (celsius * 1.8 + 32.0);
temperature = '<br/>' + celsius.toFixed(1) + '°C ' + fahrenheit.toFixed(1) + '°F';
}
return temperature;
},
connect: function(e) {
var deviceId = e.target.dataset.deviceId;
ble.connect(deviceId, app.onConnect, app.onError);
},
onConnect: function(peripheral) {
app.peripheral = peripheral;
app.showDetailPage();
var failure = function(reason) {
navigator.notification.alert(reason, null, "Temperature Error");
};
// subscribe to be notified when the temperature changes
ble.startNotification(
peripheral.id,
THERMOMETER_SERVICE,
TEMPERATURE_CHARACTERISTIC,
app.onTemperatureChange,
failure
);
// read the initial value
ble.read(
peripheral.id,
THERMOMETER_SERVICE,
TEMPERATURE_CHARACTERISTIC,
app.onTemperatureChange,
failure
);
},
onTemperatureChange: function(buffer) {
var data = new Float32Array(buffer);
var celsius = data[0];
var fahrenheit = (celsius * 1.8 + 32.0).toFixed(1);
var message = "Temperature is " + fahrenheit + " °F";
statusDiv.innerHTML = message;
},
disconnect: function(e) {
if (app.peripheral && app.peripheral.id) {
ble.disconnect(app.peripheral.id, app.showMainPage, app.onError);
}
},
showMainPage: function() {
mainPage.hidden = false;
detailPage.hidden = true;
},
showDetailPage: function() {
mainPage.hidden = true;
detailPage.hidden = false;
},
onBackButton: function() {
if (mainPage.hidden) {
app.disconnect();
} else {
navigator.app.exitApp();
}
},
onError: function(reason) {
navigator.notification.alert(reason, app.showMainPage, 'Error');
}
};
app.initialize();