-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapp.js
222 lines (174 loc) · 5.66 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/** Info.plist entries
<plist>
<dict>
<!-- Use when requiring background execution -->
<key>UIBackgroundModes</key>
<array>
<!-- Use when acting as a central -->
<string>bluetooth-central</string>
<!-- Use when acting as a peripheral -->
<string>bluetooth-peripheral</string>
</array>
<!-- Use when acting as a peripheral -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Can we connect to Bluetooth devices?</string>
</dict>
</plist>
**/
/**
Check the documentation to see which api functionalities are available for your platform.
*/
var BLE = require('ti.bluetooth');
var myPeripheral;
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var btn1 = Ti.UI.createButton({
title: 'Start scan',
top: 40
});
var centralManager = BLE.createCentralManager();
if (OS_IOS) {
var peripheralManager = BLE.createPeripheralManager();
}
btn1.addEventListener('click', function() {
if (centralManager.isScanning()) {
alert('Already scanning, please stop scan first!');
return;
} else if (centralManager.getState() != BLE.MANAGER_STATE_POWERED_ON) {
alert('The BLE manager needs to be powered on before. Call initialize().');
return;
}
centralManager.startScan();
// Optional: Search for specified services
// centralManager.startScanWithServices(['384DF4C0-8BAE-419D-9A65-2D67942C2DB7']);
});
var btn2 = Ti.UI.createButton({
title: 'Stop scan',
top: 100
});
btn2.addEventListener('click', function() {
if (!centralManager.isScanning()) {
alert('Not scanning!');
return;
}
centralManager.stopScan();
});
/**
* Central Manager Events
*/
centralManager.addEventListener('didDiscoverPeripheral', function(e) {
Ti.API.info('didDiscoverPeripheral');
Ti.API.info(e);
if (OS_ANDROID){
_.each(e.peripheral.uuids, function(item) {
console.log(e.peripheral.name, ":", e.peripheral.address, '-', item, (item.indexOf("fd6f") > -1) ? " (Exposure Notifications)" : "");
})
}
Ti.API.info('Connect to ' + e.peripheral);
centralManager.connectPeripheral(e.peripheral, {
notifyOnConnection: true,
notifyOnDisconnection: true
});
centralManager.stopScan();
});
centralManager.addEventListener('didUpdateState', function(e) {
Ti.API.info('didUpdateState');
switch (e.state) {
case BLE.MANAGER_STATE_RESETTING:
Ti.API.info('Resetting');
break;
case BLE.MANAGER_STATE_UNSUPPORTED:
Ti.API.info('Unsupported');
break;
case BLE.MANAGER_STATE_UNAUTHORIZED:
Ti.API.info('Unauthorized');
break;
case BLE.MANAGER_STATE_POWERED_OFF:
Ti.API.info('Powered Off');
break;
case BLE.MANAGER_STATE_POWERED_ON:
Ti.API.info('Powered On');
break;
case BLE.MANAGER_STATE_UNKNOWN:
default:
Ti.API.info('Unknown');
break;
}
});
centralManager.addEventListener('didConnectPeripheral', function(e) {
Ti.API.info('didConnectPeripheral');
Ti.API.info(e);
// Now you can add event listener to the found peripheral.
// Make sure to handle event listeners properly and remove them
// when you don't need them anymore
myPeripheral = e.peripheral;
myPeripheral.addEventListener('didDiscoverServices', function(e) {
Ti.API.info('didDiscoverServices');
Ti.API.info(e);
});
myPeripheral.addEventListener('didDiscoverCharacteristicsForService', function(e) {
Ti.API.info('didDiscoverCharacteristicsForService');
Ti.API.info(e);
});
myPeripheral.addEventListener('didUpdateValueForCharacteristic', function(e) {
Ti.API.info('didUpdateValueForCharacteristic');
Ti.API.info(e);
});
});
centralManager.addEventListener('didDisconnectPeripheral', function(e) {
Ti.API.info('didDisconnectPeripheral');
Ti.API.info(e);
});
centralManager.addEventListener('willRestoreState', function(e) {
Ti.API.info('willRestoreState');
Ti.API.info(e);
});
centralManager.addEventListener('didFailToConnectPeripheral', function(e) {
Ti.API.info('didFailToConnectPeripheral');
Ti.API.info(e);
});
/**
* Peripheral Manager Events
*/
if (OS_IOS) {
peripheralManager.addEventListener('didUpdateState', function(e) {
Ti.API.info('didUpdateState');
Ti.API.info(e);
});
peripheralManager.addEventListener('willRestoreState', function(e) {
Ti.API.info('willRestoreState');
Ti.API.info(e);
});
peripheralManager.addEventListener('didStartAdvertising', function(e) {
Ti.API.info('didStartAdvertising');
Ti.API.info(e);
});
peripheralManager.addEventListener('didAddService', function(e) {
Ti.API.info('didAddService');
Ti.API.info(e);
});
peripheralManager.addEventListener('didSubscribeToCharacteristic', function(e) {
Ti.API.info('didSubscribeToCharacteristic');
Ti.API.info(e);
});
peripheralManager.addEventListener('didUnsubscribeFromCharacteristic', function(e) {
Ti.API.info('didUnsubscribeFromCharacteristic');
Ti.API.info(e);
});
peripheralManager.addEventListener('didReceiveReadRequest', function(e) {
Ti.API.info('didReceiveReadRequest');
Ti.API.info(e);
});
peripheralManager.addEventListener('didReceiveWriteRequests', function(e) {
Ti.API.info('didReceiveWriteRequests');
Ti.API.info(e);
});
peripheralManager.addEventListener('readyToUpdateSubscribers', function(e) {
Ti.API.info('readyToUpdateSubscribers');
Ti.API.info(e);
});
}
win.add(btn1);
win.add(btn2);
win.open();