5555import org .greenrobot .eventbus .EventBus ;
5656import org .greenrobot .eventbus .Subscribe ;
5757import org .greenrobot .eventbus .ThreadMode ;
58+ import org .json .JSONException ;
59+ import org .json .JSONObject ;
5860
5961import java .util .ArrayList ;
6062
@@ -194,17 +196,12 @@ public void onEvent(DeviceConnectionEvent event) {
194196
195197 case ESPConstants .EVENT_DEVICE_CONNECTED :
196198
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 );
207202 }
203+ Log .e (TAG , "Device Connected Event Received" );
204+ setSecurityTypeFromVersionInfo ();
208205 break ;
209206
210207 case ESPConstants .EVENT_DEVICE_DISCONNECTED :
@@ -214,18 +211,20 @@ public void onEvent(DeviceConnectionEvent event) {
214211 Toast .makeText (AddDeviceActivity .this , "Device disconnected" , Toast .LENGTH_LONG ).show ();
215212 finish ();
216213 } else {
217- askForManualDeviceConnection ();
214+ if (!isFinishing ()) {
215+ askForManualDeviceConnection ();
216+ }
218217 }
219218 break ;
220219
221220 case ESPConstants .EVENT_DEVICE_CONNECTION_FAILED :
222221
223222 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" );
227224 } else {
228- askForManualDeviceConnection ();
225+ if (!isFinishing ()) {
226+ askForManualDeviceConnection ();
227+ }
229228 }
230229 break ;
231230 }
@@ -375,18 +374,14 @@ public void run() {
375374 if (deviceType .equals (AppConstants .DEVICE_TYPE_BLE )) {
376375
377376 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 ));
381378 } else {
382379 device .connectToDevice ();
383380 }
384381 } else if (deviceType .equals (AppConstants .DEVICE_TYPE_SOFTAP )) {
385382
386383 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 ));
390385 } else {
391386
392387 if (espDevice .getTransportType ().equals (ESPConstants .TransportType .TRANSPORT_SOFTAP )
@@ -517,10 +512,18 @@ public void onClick(DialogInterface dialog, int which) {
517512
518513 dialog .dismiss ();
519514 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 ;
524527 }
525528 } else {
526529 finish ();
@@ -545,27 +548,26 @@ public void onClick(DialogInterface dialog, int which) {
545548 private void startProvisioningFlow () {
546549
547550 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 );
549552 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 ;
554556 }
555557
556558 if (deviceType .equals (AppConstants .DEVICE_TYPE_BLE )) {
557559
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 );
560562 } else {
561563 provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_0 );
562564 }
563565 goToBLEProvisionLandingActivity (securityType );
564566
565567 } else if (deviceType .equals (AppConstants .DEVICE_TYPE_SOFTAP )) {
566568
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 );
569571 } else {
570572 provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_0 );
571573 }
@@ -586,8 +588,8 @@ public void onClick(DialogInterface dialog, int position) {
586588 switch (position ) {
587589 case 0 :
588590
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 );
591593 } else {
592594 provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_BLE , ESPConstants .SecurityType .SECURITY_0 );
593595 }
@@ -597,8 +599,8 @@ public void onClick(DialogInterface dialog, int position) {
597599
598600 case 1 :
599601
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 );
602604 } else {
603605 provisionManager .createESPDevice (ESPConstants .TransportType .TRANSPORT_SOFTAP , ESPConstants .SecurityType .SECURITY_0 );
604606 }
@@ -613,6 +615,71 @@ public void onClick(DialogInterface dialog, int position) {
613615 }
614616 }
615617
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+
616683 private void goToBLEProvisionLandingActivity (int securityType ) {
617684
618685 finish ();
@@ -646,4 +713,28 @@ private String getWifiSsid() {
646713 }
647714 return ssid ;
648715 }
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+ }
649740}
0 commit comments