55
55
import org .greenrobot .eventbus .EventBus ;
56
56
import org .greenrobot .eventbus .Subscribe ;
57
57
import org .greenrobot .eventbus .ThreadMode ;
58
+ import org .json .JSONException ;
59
+ import org .json .JSONObject ;
58
60
59
61
import java .util .ArrayList ;
60
62
@@ -194,17 +196,12 @@ public void onEvent(DeviceConnectionEvent event) {
194
196
195
197
case ESPConstants .EVENT_DEVICE_CONNECTED :
196
198
197
- Log .e (TAG , "Device Connected Event Received" );
198
- ArrayList <String > deviceCaps = espDevice .getDeviceCapabilities ();
199
-
200
- if (deviceCaps .contains ("wifi_scan" )) {
201
-
202
- goToWiFiScanActivity ();
203
-
204
- } else {
205
-
206
- goToWiFiConfigActivity ();
199
+ if (TextUtils .isEmpty (provisionManager .getEspDevice ().getUserName ())) {
200
+ String userName = sharedPreferences .getString (AppConstants .KEY_USER_NAME , AppConstants .DEFAULT_USER_NAME );
201
+ provisionManager .getEspDevice ().setUserName (userName );
207
202
}
203
+ Log .e (TAG , "Device Connected Event Received" );
204
+ setSecurityTypeFromVersionInfo ();
208
205
break ;
209
206
210
207
case ESPConstants .EVENT_DEVICE_DISCONNECTED :
@@ -214,18 +211,20 @@ public void onEvent(DeviceConnectionEvent event) {
214
211
Toast .makeText (AddDeviceActivity .this , "Device disconnected" , Toast .LENGTH_LONG ).show ();
215
212
finish ();
216
213
} else {
217
- askForManualDeviceConnection ();
214
+ if (!isFinishing ()) {
215
+ askForManualDeviceConnection ();
216
+ }
218
217
}
219
218
break ;
220
219
221
220
case ESPConstants .EVENT_DEVICE_CONNECTION_FAILED :
222
221
223
222
if (espDevice != null && espDevice .getTransportType ().equals (ESPConstants .TransportType .TRANSPORT_BLE )) {
224
-
225
- Toast .makeText (AddDeviceActivity .this , "Failed to connect with device" , Toast .LENGTH_LONG ).show ();
226
- finish ();
223
+ alertForDeviceNotSupported ("Failed to connect with device" );
227
224
} else {
228
- askForManualDeviceConnection ();
225
+ if (!isFinishing ()) {
226
+ askForManualDeviceConnection ();
227
+ }
229
228
}
230
229
break ;
231
230
}
@@ -375,18 +374,14 @@ public void run() {
375
374
if (deviceType .equals (AppConstants .DEVICE_TYPE_BLE )) {
376
375
377
376
if (espDevice != null && espDevice .getTransportType ().equals (ESPConstants .TransportType .TRANSPORT_SOFTAP )) {
378
-
379
- Toast .makeText (AddDeviceActivity .this , "Error! Device not supported" , Toast .LENGTH_LONG ).show ();
380
- finish ();
377
+ alertForDeviceNotSupported (getString (R .string .error_device_transport_not_supported ));
381
378
} else {
382
379
device .connectToDevice ();
383
380
}
384
381
} else if (deviceType .equals (AppConstants .DEVICE_TYPE_SOFTAP )) {
385
382
386
383
if (espDevice != null && espDevice .getTransportType ().equals (ESPConstants .TransportType .TRANSPORT_BLE )) {
387
-
388
- Toast .makeText (AddDeviceActivity .this , "Error! Device not supported" , Toast .LENGTH_LONG ).show ();
389
- finish ();
384
+ alertForDeviceNotSupported (getString (R .string .error_device_transport_not_supported ));
390
385
} else {
391
386
392
387
if (espDevice .getTransportType ().equals (ESPConstants .TransportType .TRANSPORT_SOFTAP )
@@ -517,10 +512,18 @@ public void onClick(DialogInterface dialog, int which) {
517
512
518
513
dialog .dismiss ();
519
514
if (espDevice != null ) {
520
- if (espDevice .getSecurityType ().equals (ESPConstants .SecurityType .SECURITY_0 )) {
521
- goToWiFiProvisionLandingActivity (0 );
522
- } else {
523
- goToWiFiProvisionLandingActivity (1 );
515
+
516
+ switch (espDevice .getSecurityType ()) {
517
+ case SECURITY_0 :
518
+ goToWiFiProvisionLandingActivity (AppConstants .SEC_TYPE_0 );
519
+ break ;
520
+ case SECURITY_1 :
521
+ goToWiFiProvisionLandingActivity (AppConstants .SEC_TYPE_1 );
522
+ break ;
523
+ case SECURITY_2 :
524
+ default :
525
+ goToWiFiProvisionLandingActivity (AppConstants .SEC_TYPE_2 );
526
+ break ;
524
527
}
525
528
} else {
526
529
finish ();
@@ -545,27 +548,26 @@ public void onClick(DialogInterface dialog, int which) {
545
548
private void startProvisioningFlow () {
546
549
547
550
String deviceType = sharedPreferences .getString (AppConstants .KEY_DEVICE_TYPES , AppConstants .DEVICE_TYPE_DEFAULT );
548
- final boolean isSec1 = sharedPreferences .getBoolean (AppConstants .KEY_SECURITY_TYPE , true );
551
+ final boolean isSecure = sharedPreferences .getBoolean (AppConstants .KEY_SECURITY_TYPE , true );
549
552
Log .d (TAG , "Device Types : " + deviceType );
550
- Log .d (TAG , "isSec1 : " + isSec1 );
551
- int securityType = 0 ;
552
- if (isSec1 ) {
553
- securityType = 1 ;
553
+ int securityType = AppConstants .SEC_TYPE_0 ;
554
+ if (isSecure ) {
555
+ securityType = AppConstants .SEC_TYPE_DEFAULT ;
554
556
}
555
557
556
558
if (deviceType .equals (AppConstants .DEVICE_TYPE_BLE )) {
557
559
558
- if (isSec1 ) {
559
- provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_1 );
560
+ if (isSecure ) {
561
+ provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_2 );
560
562
} else {
561
563
provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_0 );
562
564
}
563
565
goToBLEProvisionLandingActivity (securityType );
564
566
565
567
} else if (deviceType .equals (AppConstants .DEVICE_TYPE_SOFTAP )) {
566
568
567
- if (isSec1 ) {
568
- provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_1 );
569
+ if (isSecure ) {
570
+ provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_2 );
569
571
} else {
570
572
provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_0 );
571
573
}
@@ -586,8 +588,8 @@ public void onClick(DialogInterface dialog, int position) {
586
588
switch (position ) {
587
589
case 0 :
588
590
589
- if (isSec1 ) {
590
- provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_1 );
591
+ if (isSecure ) {
592
+ provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_2 );
591
593
} else {
592
594
provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_0 );
593
595
}
@@ -597,8 +599,8 @@ public void onClick(DialogInterface dialog, int position) {
597
599
598
600
case 1 :
599
601
600
- if (isSec1 ) {
601
- provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_1 );
602
+ if (isSecure ) {
603
+ provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_2 );
602
604
} else {
603
605
provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_0 );
604
606
}
@@ -613,6 +615,71 @@ public void onClick(DialogInterface dialog, int position) {
613
615
}
614
616
}
615
617
618
+ private void setSecurityTypeFromVersionInfo () {
619
+
620
+ boolean isSecure = sharedPreferences .getBoolean (AppConstants .KEY_SECURITY_TYPE , true );
621
+ String protoVerStr = provisionManager .getEspDevice ().getVersionInfo ();
622
+ int securityType = AppConstants .SEC_TYPE_2 ;
623
+
624
+ try {
625
+ JSONObject jsonObject = new JSONObject (protoVerStr );
626
+ JSONObject provInfo = jsonObject .getJSONObject ("prov" );
627
+
628
+ if (provInfo .has ("sec_ver" )) {
629
+
630
+ int serVer = provInfo .optInt ("sec_ver" );
631
+ Log .d (TAG , "Security Version : " + serVer );
632
+
633
+ switch (serVer ) {
634
+ case AppConstants .SEC_TYPE_0 :
635
+ securityType = AppConstants .SEC_TYPE_0 ;
636
+ provisionManager .getEspDevice ().setSecurityType (ESPConstants .SecurityType .SECURITY_0 );
637
+ break ;
638
+ case AppConstants .SEC_TYPE_1 :
639
+ securityType = AppConstants .SEC_TYPE_1 ;
640
+ provisionManager .getEspDevice ().setSecurityType (ESPConstants .SecurityType .SECURITY_1 );
641
+ break ;
642
+ case AppConstants .SEC_TYPE_2 :
643
+ default :
644
+ securityType = AppConstants .SEC_TYPE_2 ;
645
+ provisionManager .getEspDevice ().setSecurityType (ESPConstants .SecurityType .SECURITY_2 );
646
+ break ;
647
+ }
648
+ } else {
649
+ if (securityType == AppConstants .SEC_TYPE_2 ) {
650
+ securityType = AppConstants .SEC_TYPE_1 ;
651
+ provisionManager .getEspDevice ().setSecurityType (ESPConstants .SecurityType .SECURITY_1 );
652
+ }
653
+ }
654
+ } catch (JSONException e ) {
655
+ e .printStackTrace ();
656
+ Log .d (TAG , "Capabilities JSON not available." );
657
+ }
658
+
659
+ if (isSecure ) {
660
+ if (securityType == AppConstants .SEC_TYPE_0 ) {
661
+ alertForDeviceNotSupported (getString (R .string .error_security_mismatch ));
662
+ } else {
663
+ processDeviceCapabilities ();
664
+ }
665
+ } else {
666
+ if (securityType != AppConstants .SEC_TYPE_0 ) {
667
+ alertForDeviceNotSupported (getString (R .string .error_security_mismatch ));
668
+ } else {
669
+ processDeviceCapabilities ();
670
+ }
671
+ }
672
+ }
673
+
674
+ private void processDeviceCapabilities () {
675
+ ArrayList <String > deviceCaps = espDevice .getDeviceCapabilities ();
676
+ if (deviceCaps .contains ("wifi_scan" )) {
677
+ goToWiFiScanActivity ();
678
+ } else {
679
+ goToWiFiConfigActivity ();
680
+ }
681
+ }
682
+
616
683
private void goToBLEProvisionLandingActivity (int securityType ) {
617
684
618
685
finish ();
@@ -646,4 +713,28 @@ private String getWifiSsid() {
646
713
}
647
714
return ssid ;
648
715
}
716
+
717
+ private void alertForDeviceNotSupported (String msg ) {
718
+
719
+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
720
+ builder .setCancelable (false );
721
+
722
+ builder .setTitle (R .string .error_title );
723
+ builder .setMessage (msg );
724
+
725
+ // Set up the buttons
726
+ builder .setPositiveButton (R .string .btn_ok , new DialogInterface .OnClickListener () {
727
+
728
+ @ Override
729
+ public void onClick (DialogInterface dialog , int which ) {
730
+ if (provisionManager .getEspDevice () != null ) {
731
+ provisionManager .getEspDevice ().disconnectDevice ();
732
+ }
733
+ dialog .dismiss ();
734
+ finish ();
735
+ }
736
+ });
737
+
738
+ builder .show ();
739
+ }
649
740
}
0 commit comments