0

I'm trying to access ESP8266 over internet and from LAN, and i'm using no-ip to get connected over internet but if i have no internet connection i can not access ESP8266 so i have to connect to LAN if there is no internet connection. I read forums here and almost tried all but unable to access ESP8266 through LAN.

String address = "mynoipdomain"; //global variable
String lan = "myesplocalip"; // global variable

String serverAdress;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    assert connectivityManager != null; //i added this line because Android Studio was giving warning that "Method invocation 'getNetworkInfo' may produce 'java.lang.NullPointerException'"
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        serverAdress = address + ":3001"; //3001 is port
    }
    else{
        serverAdress = lan + ":3001";
    }
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I feel like it's a port forwarding issue, – mustafa96m Sep 18 '18 at 21:42
  • Nope i accessed ESP8266 by entering its local ip + port from browser so its not port forwarding issue. My application is working fine if i use noip domain or local ip on at a time. But unable to use the above code it is continuously accessing by no ip domain not switching to lan. – Ahmed Ansari Sep 18 '18 at 21:47
  • Then I believe the logic you are following above is not correct. I guess you need to have different internet checking mechanism instead of checking mobile/WiFi connectivity because you might be connected to the network but the host is not necessarily reachable – mustafa96m Sep 18 '18 at 21:50
  • check this [thread](https://stackoverflow.com/questions/9570237/android-check-internet-connection) – mustafa96m Sep 18 '18 at 21:53
  • @mustafa96m thank you for your response – Ahmed Ansari Sep 18 '18 at 22:09

1 Answers1

0

the if condition probably should be:

String bssid = getCurrentSsid(getContext());

if(bssid != null && bssid.equals("HOME_BSSID")) {
    serverAdress = lan + ":3001";
} else if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED) {
    serverAdress = address + ":3001";
} else {
    // both networks are offline
}

combined with this method (source):

public String getCurrentSsid(Context context) {
    String ssid = null;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo == null) {
        return null;
    }

    if (networkInfo.isConnected()) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
            ssid = connectionInfo.getSSID();
        }
    }
    return ssid;
}

in order to prefer the local IP, when the home WiFi is connected.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • are you sure it will switch to no ip domain if no wifi connected? i am not having another connection at the moment so can't test it right now. and what if my android is connected to some other wifi not my mobile network it will then not find my local ip – Ahmed Ansari Sep 18 '18 at 21:57
  • @AhmedAnsari you could still extend the condition, eg. to check for the `BSSID`. it is in any case better than the condition in the example, if you compare the logic A to B, once. – Martin Zeitler Sep 18 '18 at 21:58
  • @AhmedAnsari this answers how to identify the WiFi: https://stackoverflow.com/a/36926506/549372 (in order to reliably determine the home network). while your condition will strictly connect to no-IP, when either of the networks is connected... `||` is logical `OR`, while `&&` is logical `AND`. – Martin Zeitler Sep 18 '18 at 22:05
  • `else if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED){ serverAdress = address + ":3001"; }` @Martin Is that right i added in else if condition? – Ahmed Ansari Sep 18 '18 at 22:08
  • @AhmedAnsari yes, in case the first `if` checks for Wifi `&&` the `BSSID` of your home network. – Martin Zeitler Sep 18 '18 at 22:10
  • i'm bit confused with the link you provided. can you extend your above code with that. Sorry for all that actually i m a beginner in Android. – Ahmed Ansari Sep 18 '18 at 22:19
  • @AhmedAnsari updated, while `"HOME_BSSID"` should be replaced. hope this explains it better now. – Martin Zeitler Sep 18 '18 at 23:07
  • Thank you so much. – Ahmed Ansari Sep 18 '18 at 23:09