/
SDKExampleActivity.java
390 lines (279 loc) · 10.6 KB
/
SDKExampleActivity.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.aurorasdk.app;
import com.aurorasdk.Aurora;
import com.aurorasdk.Command;
import com.aurorasdk.CommandSdFileRead;
import com.aurorasdk.CommandSdFileWrite;
import com.aurorasdk.CommandProfileLoad;
import com.aurorasdk.CommandProfileUnload;
import com.aurorasdk.CommandTimeSync;
import com.aurorasdk.Profile;
import com.aurorasdk.Event;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public class SDKExampleActivity extends AppCompatActivity {
private final static String TAG = SDKExampleActivity.class.getSimpleName();
private final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
private TextView txt_status;
private EditText txt_command;
private EditText txt_response;
private EditText txt_event;
private ProgressBar prgs_bleActive;
private Aurora aurora;
/* Lifecycle
------------------------------------------------------------------------------------------------
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sdkexample);
txt_status = (TextView) findViewById(R.id.statusTextView);
txt_command = (EditText) findViewById(R.id.txtCommand);
txt_response = (EditText) findViewById(R.id.responseText);
txt_event = (EditText) findViewById(R.id.eventText);
prgs_bleActive = (ProgressBar) findViewById(R.id.progressBarBleActive);
if (Build.VERSION.SDK_INT >= 23) {
// Marshmallow+ Permission APIs
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final List<String> permissionsList = new ArrayList<String>();
permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
aurora = Aurora.create(this, this::onConnectionStateChange, this::onError);
aurora.setDebug(true);
}
/* Button Click Handlers
------------------------------------------------------------------------------------------------
*/
public void onStartScanClick(View v) {
aurora.startScan(scanResults -> {
if (!aurora.isConnected()){
showMsg("Auroras found: " + scanResults.size());
}
});
}
public void onStopScanClick(View v)
{
aurora.stopScan();
}
public void onConnectClick(View v) {
if (!aurora.isConnected()) {
aurora.connect(this::onCommandComplete);
}
}
public void onDisconnectClick(View v) {
aurora.disconnect();
}
public void onCustomCommandClick(View v) {
String command = txt_command.getText().toString();
if (!command.isEmpty()) {
this.queueCommand(command);
}
}
public void onLedDemoClick(View v){
queueCommand("led-demo");
}
public void onSyncTimeClick(View v){
queueCommand(new CommandTimeSync());
}
public void onOsInfoClick(View v){
queueCommand("os-info");
}
public void onWriteFileClick(View v) {
String content = "#profile loading\n" +
"[prof-monitor=0]\n" +
"stream-config 18 16 {stim-delay:14400000}\n" +
"stream-config 18 17 {stim-interval:300000}\n" +
"stream-config 18 19 {stim-enabled:0}\n" +
"stream-config 18 21 {wakeup-window:1800000} \n" +
"stream-config 18 23 {sa-enabled:0}\n" +
"stream-config 18 25 {dsl-enabled:0} \n" +
"stream-config 18 27 {wakeup-time:28800000} \n" +
"event-output-enable 0x0001007F 0x04\n" +
"stream-debug {stream-debug:0}\n" +
"stream-session 1\n" +
"stream-output-enable 0x01070003 0x01\n" +
"stream-output-enable {file-streams:0x01060403} {file-output:0x04}\n" +
"\n" +
"#profile unloading\n" +
"[prof-monitor=2]\n" +
"stream-output-disable 0xFFFFFFFF 0xFF\n" +
"stream-session 0\n" +
"event-output-disable 0x0001006E 0x04\n" +
"stream-config 18 27 -1\n" +
"stream-config 18 25 0\n" +
"stream-config 18 23 0\n" +
"stream-config 18 19 0\n" +
"\n" +
"#button down\n" +
"[button-monitor@0]\n" +
"led-set 0x03 0xFF00FF 0x7F -1\n" +
"\n" +
"#button up\n" +
"[button-monitor@1]\n" +
"led-set 0x03 0 0 -1\n" +
"\n" +
"[stim-presented]\n" +
"{stim-led:led-blink 0x03 0xFF0000 0xFF 5 500 0}\n" +
"{stim-buzz:}\n" +
"\n" +
"[wakeup-alarm]\n" +
"{wakeup-led:}\n" +
"{wakeup-buzz:}";
queueCommand(new CommandSdFileWrite("test.txt", content));
}
public void onReadFileClick(View v)
{
//queueCommand(new CommandSdFileRead("test.txt"));
queueCommand(new CommandSdFileRead("sessions/2018-05-22@2158/session.txt"));
}
public void onLoadProfileClick(View v) {
queueCommand(new CommandProfileLoad("default.prof"));
}
public void onUnloadProfileClick(View v)
{
queueCommand(new CommandProfileUnload());
}
public void onUpdateProfileClick(View v){
queueCommand(new CommandSdFileRead("profiles/default.prof"), (readCmd) -> {
if (!readCmd.hasError()){
Profile profile = new Profile(readCmd.getResponseOutputString());
profile.setOptionValue(Profile.Option.STIM_ENABLED, profile.getOptions().get(Profile.Option.STIM_ENABLED) == "0");
queueCommand(new CommandSdFileWrite("profiles/default.prof", profile.toString()));
}
});
}
public void onEnableEventsClick(View v) {
if (aurora.isConnected()){
showMsg("Enabling events...");
aurora.enableEvents(EnumSet.of(Event.EventType.BUTTON_MONITOR, Event.EventType.BATTERY_MONITOR), this::onEvent);
}
else {
showMsg("Aurora must be connected to enable events.");
}
}
public void onDisableEventsClick(View v) {
if (aurora.isConnected()){
showMsg("Disabling events...");
aurora.disableEvents(EnumSet.of(Event.EventType.BUTTON_MONITOR, Event.EventType.BATTERY_MONITOR));
}
else {
showMsg("Aurora must be connected to disable events.");
}
}
public void onUseIndicationsClick(View v) {
if (aurora.isConnected()){
showMsg("Now using indications for command output.");
aurora.useIndicationsForCommandOutput();
}
else {
showMsg("Aurora must first be connected before switching to indications.");
}
}
/* Helper methods
------------------------------------------------------------------------------------------------
*/
private void showMsg(String msg) {
Log.d(TAG, msg);
runOnUiThread(() -> {
txt_status.setText(msg);
});
}
private void queueCommand(Command command, Command.CommandCompletionListener completionListener){
if (aurora.isConnected()){
showMsg("Queuing command: " + command.getCommandString());
aurora.queueCommand(command, completionListener);
}
else {
showMsg("Dreamband not connected, command canceled.");
}
}
private void queueCommand(Command command){
queueCommand(command, null);
}
private void queueCommand(String commandString){
Command command = new Command();
command.setCommandString(commandString);
queueCommand(command);
}
/* Event Handlers
------------------------------------------------------------------------------------------------
*/
private void onCommandComplete(Command command){
runOnUiThread(() -> {
prgs_bleActive.setVisibility(View.INVISIBLE);
if (command.hasError()){
showMsg("Command error: " + command.getErrorMessage());
}
else {
showMsg("Command completed successfully.");
}
txt_response.setText(command.isTable() ? command.getResponseTable().toString() : command.getResponseObject().toString());
});
}
private void onConnectionStateChange(Aurora.ConnectionState connectionState){
final String message;
boolean busy = false;
switch (connectionState){
case IDLE:
message = "Touch SCAN or CONNECT to begin.";
break;
case SCANNING:
message = "Scanning for Aurora devices...";
busy = true;
break;
case CONNECTING:
message = "Connecting to Aurora device...";
busy = true;
break;
case CONNECTED:
message = "Connected to Aurora.";
break;
case DISCONNECTING:
message = "Disconnecting from Aurora...";
busy = true;
break;
case DISCONNECTED:
message = "Disconnected from Aurora.";
break;
case RECONNECTING:
message = "Attempting to reconnect to Aurora...";
busy = true;
break;
default:
message = "";
break;
}
showMsg(message);
if (busy){
runOnUiThread(() -> {
prgs_bleActive.setVisibility(View.VISIBLE);
});
}
else {
runOnUiThread(() -> {
prgs_bleActive.setVisibility(View.INVISIBLE);
});
}
}
private void onEvent(Event event){
runOnUiThread(() -> {
txt_event.setText(event.toString());
});
}
private void onError(Aurora.ErrorType errorType, String errorMessage){
showMsg(errorType.name() + ": " + errorMessage);
}
}