23
23
import android .content .Context ;
24
24
import android .content .DialogInterface ;
25
25
import android .content .Intent ;
26
+ import android .content .SharedPreferences ;
26
27
import android .content .pm .PackageManager ;
27
28
import android .os .Build ;
28
29
import android .os .Bundle ;
34
35
import android .support .v7 .widget .Toolbar ;
35
36
import android .util .Log ;
36
37
import android .view .HapticFeedbackConstants ;
37
- import android .view .LayoutInflater ;
38
38
import android .view .View ;
39
39
import android .widget .AdapterView ;
40
- import android .widget .EditText ;
41
40
import android .widget .ImageView ;
42
41
import android .widget .ListView ;
43
42
import android .widget .ProgressBar ;
71
70
72
71
import avs .Avsconfig ;
73
72
74
- import static com .espressif .avs .ConfigureAVS .AVS_CONFIG_PATH ;
75
-
76
73
public class BLEProvisionLanding extends AppCompatActivity {
77
74
78
75
private static final String TAG = "Espressif::" + BLEProvisionLanding .class .getSimpleName ();
@@ -95,7 +92,6 @@ public class BLEProvisionLanding extends AppCompatActivity {
95
92
private Security security ;
96
93
public static BLETransport bleTransport ;
97
94
private BLETransport .BLETransportListener transportListener ;
98
- // FIXME : Remove static BLE_TRANSPORT and think for another solution.
99
95
100
96
private String deviceName ;
101
97
private String deviceNamePrefix ;
@@ -108,6 +104,7 @@ public class BLEProvisionLanding extends AppCompatActivity {
108
104
private BLEScanner bleScanner ;
109
105
private ArrayList <BluetoothDevice > deviceList ;
110
106
private HashMap <BluetoothDevice , EspBtDevice > bluetoothDevices ;
107
+ private SharedPreferences sharedPreferences ;
111
108
private Handler handler ;
112
109
113
110
@ Override
@@ -136,14 +133,14 @@ protected void onCreate(Bundle savedInstanceState) {
136
133
return ;
137
134
}
138
135
139
- deviceNamePrefix = getIntent ().getStringExtra (AppConstants .KEY_BLE_DEVICE_NAME_PREFIX );
140
-
141
136
isConnecting = false ;
142
137
isDeviceConnected = false ;
143
138
handler = new Handler ();
144
139
bluetoothDevices = new HashMap <>();
145
140
Collection <BluetoothDevice > keySet = bluetoothDevices .keySet ();
146
141
deviceList = new ArrayList <>(keySet );
142
+ sharedPreferences = getSharedPreferences (AppConstants .ESP_PREFERENCES , Context .MODE_PRIVATE );
143
+ deviceNamePrefix = sharedPreferences .getString (AppConstants .KEY_BLE_DEVICE_NAME_PREFIX , "" );
147
144
148
145
initViews ();
149
146
bleScanner = new BLEScanner (this , SCAN_TIMEOUT , bleScanListener );
@@ -209,7 +206,9 @@ protected void onResume() {
209
206
}
210
207
211
208
if (isBleWorkDone ) {
212
- bleTransport .disconnect ();
209
+ if (bleTransport != null ) {
210
+ bleTransport .disconnect ();
211
+ }
213
212
btnScan .setVisibility (View .VISIBLE );
214
213
startScan ();
215
214
}
@@ -219,7 +218,9 @@ protected void onResume() {
219
218
@ Override
220
219
public void onBackPressed () {
221
220
isBleWorkDone = true ;
222
- bleTransport .disconnect ();
221
+ if (bleTransport != null ) {
222
+ bleTransport .disconnect ();
223
+ }
223
224
super .onBackPressed ();
224
225
}
225
226
@@ -344,6 +345,7 @@ private void stopScan() {
344
345
private void bleDeviceConfigured (final Boolean isConfigured ) {
345
346
346
347
runOnUiThread (new Runnable () {
348
+
347
349
@ Override
348
350
public void run () {
349
351
@@ -357,7 +359,7 @@ public void run() {
357
359
358
360
if (!bleTransport .deviceCapabilities .contains ("no_pop" ) && securityVersion .equals (Provision .CONFIG_SECURITY_SECURITY1 )) {
359
361
360
- askForPop ();
362
+ goToProofOfPossessionActivity ();
361
363
}
362
364
} else {
363
365
pop = getResources ().getString (R .string .proof_of_possesion );
@@ -393,14 +395,6 @@ private void updateProgressAndScanBtn() {
393
395
}
394
396
}
395
397
396
- private void goToProofOfPossessionActivity () {
397
-
398
- Intent alexaProvisioningIntent = new Intent (getApplicationContext (), ProofOfPossessionActivity .class );
399
- alexaProvisioningIntent .putExtras (getIntent ());
400
- alexaProvisioningIntent .putExtra (LoginWithAmazon .KEY_IS_PROVISIONING , true );
401
- startActivity (alexaProvisioningIntent );
402
- }
403
-
404
398
private void getStatus () {
405
399
406
400
Avsconfig .CmdSignInStatus configRequest = Avsconfig .CmdSignInStatus .newBuilder ()
@@ -414,7 +408,7 @@ private void getStatus() {
414
408
415
409
byte [] message = security .encrypt (payload .toByteArray ());
416
410
417
- BLEProvisionLanding . bleTransport .sendConfigData (AVS_CONFIG_PATH , message , new ResponseListener () {
411
+ bleTransport .sendConfigData (AppConstants . HANDLER_AVS_CONFIG , message , new ResponseListener () {
418
412
419
413
@ Override
420
414
public void onSuccess (byte [] returnData ) {
@@ -424,7 +418,7 @@ public void onSuccess(byte[] returnData) {
424
418
425
419
if (deviceStatus .equals (Avsconfig .AVSConfigStatus .SignedIn )) {
426
420
427
- goToProvisionActivity ();
421
+ goToWiFiScanActivity ();
428
422
429
423
} else {
430
424
goToLoginActivity ();
@@ -457,6 +451,15 @@ private Avsconfig.AVSConfigStatus processSignInStatusResponse(byte[] responseDat
457
451
return status ;
458
452
}
459
453
454
+ private void goToProofOfPossessionActivity () {
455
+
456
+ Intent alexaProvisioningIntent = new Intent (getApplicationContext (), ProofOfPossessionActivity .class );
457
+ alexaProvisioningIntent .putExtras (getIntent ());
458
+ alexaProvisioningIntent .putExtra (LoginWithAmazon .KEY_IS_PROVISIONING , true );
459
+ alexaProvisioningIntent .putExtra (LoginWithAmazon .KEY_DEVICE_NAME , deviceName );
460
+ startActivity (alexaProvisioningIntent );
461
+ }
462
+
460
463
private void goToLoginActivity () {
461
464
462
465
Intent alexaProvisioningIntent = new Intent (getApplicationContext (), LoginWithAmazon .class );
@@ -467,7 +470,7 @@ private void goToLoginActivity() {
467
470
startActivity (alexaProvisioningIntent );
468
471
}
469
472
470
- private void goToProvisionActivity () {
473
+ private void goToWiFiScanActivity () {
471
474
472
475
Intent launchProvisionInstructions = new Intent (getApplicationContext (), WiFiScanActivity .class );
473
476
launchProvisionInstructions .putExtras (getIntent ());
@@ -506,13 +509,13 @@ public void OnSessionEstablishFailed(Exception e) {
506
509
session .init (null );
507
510
}
508
511
509
- private void alertForDeviceNotSupported () {
512
+ private void alertForDeviceConnectionFailed () {
510
513
511
514
AlertDialog .Builder builder = new AlertDialog .Builder (this );
512
515
builder .setCancelable (false );
513
516
514
517
builder .setTitle ("Error!" );
515
- builder .setMessage (R .string .error_device_not_supported );
518
+ builder .setMessage (R .string .error_device_connection_failed );
516
519
517
520
// Set up the buttons
518
521
builder .setPositiveButton (R .string .btn_ok , new DialogInterface .OnClickListener () {
@@ -632,42 +635,10 @@ public void run() {
632
635
bleTransport .disconnect ();
633
636
}
634
637
progressBar .setVisibility (View .GONE );
635
- alertForDeviceNotSupported ();
638
+ alertForDeviceConnectionFailed ();
636
639
}
637
640
};
638
641
639
- private void askForPop () {
640
-
641
- AlertDialog .Builder builder = new AlertDialog .Builder (this );
642
- builder .setCancelable (false );
643
-
644
- LayoutInflater layoutInflaterAndroid = LayoutInflater .from (this );
645
- View view = layoutInflaterAndroid .inflate (R .layout .dialog_pop , null );
646
- builder .setView (view );
647
- final EditText etPop = view .findViewById (R .id .et_prefix );
648
- etPop .setHint (R .string .hint_txt_pop );
649
- etPop .setText (getResources ().getString (R .string .proof_of_possesion ));
650
- etPop .setSelection (etPop .getText ().length ());
651
-
652
- // Set up the buttons
653
- builder .setPositiveButton (R .string .btn_save , new DialogInterface .OnClickListener () {
654
-
655
- @ Override
656
- public void onClick (DialogInterface dialog , int which ) {
657
-
658
- pop = etPop .getText ().toString ();
659
-
660
- if (pop != null ) {
661
- pop = pop .trim ();
662
- }
663
-
664
- initSession ();
665
- }
666
- });
667
-
668
- builder .show ();
669
- }
670
-
671
642
private boolean parseAdvertisementPacket (final byte [] scanRecord ) {
672
643
673
644
byte [] advertisedData = Arrays .copyOf (scanRecord , scanRecord .length );
0 commit comments