-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
120 lines (110 loc) · 3.81 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
var COUNTER_SERVICE = '1234';
var COUNT_CHARACTERISTIC = '5678';
var app = {
initialize: function() {
this.bindEvents();
this.showMainPage();
scanButton.hidden = true;
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('backbutton', this.onBackButton, false);
scanButton.addEventListener('click', this.scan, false);
disconnectButton.addEventListener('click', this.disconnect, false);
},
onDeviceReady: function() {
// initial scan is on a timer, so there's time to background
// the app after it starts
setTimeout(app.scan, 3000);
},
scan: function() {
scanButton.hidden = true;
ble.startScan([COUNTER_SERVICE], app.onDiscoverDevice, app.onError);
},
onDiscoverDevice: function(device) {
console.log(JSON.stringify(device));
// if we're in the background, add a local notification
var localNotification = {
title: 'Found Bluetooth Device',
text: device.name + ' (' + device.id + ')',
};
cordova.plugins.notification.local.schedule(localNotification);
// stop scanning and connect automatically
ble.stopScan(
function() {
ble.connect(device.id, app.onConnect, app.onDisconnect);
},
function(reason) {
console.log('Error stopping scan', reason);
}
);
},
onConnect: function(peripheral) {
app.peripheral = peripheral;
deviceIdDiv.innerText = 'Connected to ' + peripheral.id;
app.showDetailPage();
app.subscribeToNotifications();
},
subscribeToNotifications: function() {
ble.startNotification(
app.peripheral.id,
COUNTER_SERVICE,
COUNT_CHARACTERISTIC,
app.onData,
app.onError);
},
onData: function(buffer) {
console.log("onData");
var data = new Uint32Array(buffer);
countDiv.innerText = data[0];
console.log(data[0]);
var localNotification = {
title: 'Count',
text: 'The count is ' + data[0]
};
cordova.plugins.notification.local.schedule(localNotification);
},
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;
scanButton.hidden = false;
},
showDetailPage: function() {
mainPage.hidden = true;
detailPage.hidden = false;
},
onBackButton: function() {
if (mainPage.hidden) {
app.disconnect();
} else {
navigator.app.exitApp();
}
},
onDisconnect: function(peripheral) {
// onDisconnect is called when the peripheral disconnects from us,
// but not when we call app.disconnect()
var message;
if (typeof peripheral === 'object') {
// usually we get an object for the peripheral that disconnected
message = 'Peripheral ' + (peripheral.name || peripheral.id) + ' disconnected';
} else {
// sometimes we get an error message
message = peripheral;
}
navigator.notification.alert(message, app.showMainPage, 'Disconnected');
},
onError: function(reason) {
if (typeof reason === 'object') {
var device = reason;
navigator.notification.alert('Device ' + device.id + ' disconnected', app.showMainPage, 'Disconnected');
} else {
navigator.notification.alert(reason, app.showMainPage, 'Error');
}
}
};
app.initialize();