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";
}