22
33import static io .pslab .others .ScienceLabCommon .scienceLab ;
44
5+ import android .app .Activity ;
56import android .graphics .Bitmap ;
67import android .graphics .Paint ;
8+ import android .os .AsyncTask ;
79import android .os .Bundle ;
10+ import android .util .Log ;
811import android .view .KeyEvent ;
912import android .view .LayoutInflater ;
1013import android .view .View ;
3235import butterknife .Unbinder ;
3336import io .pslab .R ;
3437import io .pslab .activity .MainActivity ;
38+ import io .pslab .communication .SocketClient ;
3539import io .pslab .others .CustomSnackBar ;
3640import io .pslab .others .InitializationVariable ;
3741import io .pslab .others .PSLabPermission ;
@@ -105,8 +109,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
105109 }
106110 imgViewDeviceStatus .setImageResource (R .drawable .icons8_usb_connected_100 );
107111 tvDeviceStatus .setText (getString (R .string .device_connected_successfully ));
108- }
109- else if (!deviceFound && deviceConnected ) {
112+ } else if (!deviceFound && deviceConnected ) {
110113 tvConnectMsg .setVisibility (View .GONE );
111114 try {
112115 tvVersion .setText (scienceLab .getVersion ());
@@ -119,8 +122,7 @@ else if (!deviceFound && deviceConnected) {
119122 requireActivity ().invalidateOptionsMenu ();
120123 CustomSnackBar .showSnackBar (requireActivity ().findViewById (android .R .id .content ),
121124 getString (R .string .device_connected_successfully ), null , null , Snackbar .LENGTH_SHORT );
122- }
123- else {
125+ } else {
124126 imgViewDeviceStatus .setImageResource (R .drawable .icons_usb_disconnected_100 );
125127 tvDeviceStatus .setText (getString (R .string .device_not_found ));
126128 }
@@ -207,14 +209,50 @@ public void onClick(View v) {
207209 wifiButton .setOnClickListener (new View .OnClickListener () {
208210 @ Override
209211 public void onClick (View v ) {
210- ESPFragment espFragment = new ESPFragment ();
211- espFragment .show (getActivity ().getSupportFragmentManager (), "wifi" );
212- espFragment .setCancelable (true );
212+ new ESPTask ().execute ();
213213 }
214214 });
215215 return view ;
216216 }
217217
218+ private class ESPTask extends AsyncTask <Void , Void , Boolean > {
219+
220+ @ Override
221+ protected void onPreExecute () {
222+ /**/
223+ }
224+
225+ @ Override
226+ protected Boolean doInBackground (Void ... voids ) {
227+ try {
228+ SocketClient socketClient = SocketClient .getInstance ();
229+ socketClient .openConnection ("192.168.4.1" , 80 );
230+ if (socketClient .isConnected ()) {
231+ ScienceLabCommon .setIsWifiConnected (true );
232+ ScienceLabCommon .setEspBaseIP ("192.168.4.1" );
233+ return true ;
234+ }
235+ } catch (Exception e ) {
236+ e .printStackTrace ();
237+ }
238+ return false ;
239+ }
240+
241+ @ Override
242+ protected void onPostExecute (Boolean result ) {
243+ Activity activity ;
244+ if (!result && ((activity = getActivity ()) != null )) {
245+ CustomSnackBar .showSnackBar (activity .findViewById (android .R .id .content ),
246+ getString (R .string .incorrect_IP_address_message ), null , null , Snackbar .LENGTH_SHORT );
247+ } else {
248+ Log .v ("ESPFragment" , "ESP Connection Successful" );
249+ ScienceLabCommon .getInstance ().openDevice (null );
250+ ScienceLabCommon .isWifiConnected = true ;
251+ getParentFragmentManager ().beginTransaction ().replace (R .id .frame , HomeFragment .newInstance (true , false )).commitAllowingStateLoss ();
252+ }
253+ }
254+ }
255+
218256 public void hideWebView () {
219257 webView .setVisibility (View .GONE );
220258 svHomeContent .setVisibility (View .VISIBLE );
0 commit comments