2
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

result in logs

android.net.conn.CONNECTIVITY_CHANGE

getConnectivityStatusString=TYPE_ETHERNET

activeNetwork.getTypeName()=ETHERNET

activeNetwork.isConnected()=true

activeNetwork.isConnectedOrConnecting()=true

activeNetwork.isAvailable()=true

activeNetwork.getState()=CONNECTED

i am working with android printer and i am checking the Ethernet connection status. i get always same ablove results. when cable plugin and pulled out. Any idea on how to track it

Community
  • 1
  • 1
Kasup Sri
  • 159
  • 6

1 Answers1

1

For check Ethernet connection use this:

public Boolean isEthernetConnected(){
    if(isNetworkAvailable()){
        ConnectivityManager cm 
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET);
    }
    return false;
}

Here is isNetworkAvailable()

private Boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49