forked from blueappio/hexiwear-beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (60 loc) · 2.1 KB
/
app.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
angular.module('BlueAppDemo', [])
.controller('MainController', ['$scope', mainController]);
function mainController($scope) {
var main = this;
main.buttonClicked = function() {
if (isBluetoothEnabled()) {
let options = {
filters: [{name: 'HEXIWEAR'}],
optionalServices: ['0000180a-0000-1000-8000-00805f9b34fb']
};
navigator.bluetooth.requestDevice(options)
.then(function(device) {
// Receives device user selected.
main.Name = device.name; // Store name
main.Id = device.uuid ; // Store id
$scope.$apply();
return device.gatt.connect();
})
.then(function(server) {
// Device has connected.
// Get the service we are looking for to get data from.
return server.getPrimaryService('0000180a-0000-1000-8000-00805f9b34fb');
})
.then(function(service) {
// Got the service
// Get the characteristic where data is found.
return service.getCharacteristic('00002a29-0000-1000-8000-00805f9b34fb');
})
.then(function(c1) {
// Got characteristic.
// Read the value we want.
return c1.readValue();
})
.then(function(data) {
// Got the value. Let's use it in our application.
main.Manufacturer = dataToString(data);
$scope.$apply();
})
.catch(function(error) {
console.log('Argh! ' + error);
});
}
}
function isBluetoothEnabled() {
if (navigator.bluetooth) {
console.log("We have bluetooth");
return true;
} else {
return false;
}
}
function dataToString(data) {
var value = '';
for (var i = 0; i < data.byteLength; i++) {
value = value + String.fromCharCode(data.getUint8(i));
}
value = value.replace(/\0/g, '');
return value.trim();
}
}