-2

I'm writing a iptv setup box launcher in android and I'm trying to find out the ethernet connectivity information(that whether user is connected with ethernet cable or not), Please help ... My apology if question is not clear. Please help i need it badly, stucked from last three days.

2 Answers2

0

You can check if your are connected in Ethernet or in Wifi with ConnectivityManager :

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo ethernet = connManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

// get the ethernet state with : ethernet.getState()
// get the Wifi state with : wifi.getState()

You need to add in your manifest :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

You can register to connectivity events via a BroadcastReceiver, check this example

Or you can use this library that makes it for you

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
0
private Boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

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

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

Then :

if(isEthernetConnected()){
    // connected
}
else {
    //not connected
}
JoeBilly
  • 3,057
  • 29
  • 35
Shakti Singh
  • 38
  • 1
  • 1
  • 9