Android Open Source - Cardeto Network Utils






From Project

Back to project page Cardeto.

License

The source code is released under:

Apache License

If you think the Android project Cardeto listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggt.cardeto.utils;
/*from  ww  w. j  av  a  2  s .  c o m*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * Network utils. Detect if wifi is activated. Get ip address on wifi network,
 * etc ...
 *
 * @author guiguito
 */
public class NetworkUtils {

    public static class WifiDetails {
        public String ipAddress;
        public String ssid;
    }

    private static final String LOG_TAG = "NetworkUtils";

    public static WifiDetails getWifiDetails(Context context) {
        WifiDetails wifiDetails = null;
        if (isWifiApActivated(context)) {
            wifiDetails = new NetworkUtils.WifiDetails();
            // phone as HotSpot case
            WifiConfiguration wifiConfiguration = getWifiApConfiguration(context);
            if (wifiConfiguration != null) {
                wifiDetails.ssid = wifiConfiguration.SSID;
                wifiDetails.ipAddress = getWifiApIpAddress();
            }
        } else {
            // phone maybe connected on wifi normally
            ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (networkInfo.isConnected()) {
                wifiDetails = new NetworkUtils.WifiDetails();
                final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
                    wifiDetails.ssid = connectionInfo.getSSID();
                }
                if (connectionInfo != null) {
                    wifiDetails.ipAddress = convertInToIpAddress(connectionInfo.getIpAddress());
                }
                return wifiDetails;
            }
        }

        return wifiDetails;
    }

    private static String getWifiApIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                if (intf.getName().contains("wlan")) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
                            return inetAddress.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }

    private static WifiConfiguration getWifiApConfiguration(Context context) {
        WifiConfiguration wifiConfiguration = null;
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            Method method = manager.getClass().getDeclaredMethod("getWifiApConfiguration");
            method.setAccessible(true); // in the case of visibility change in
            // future APIs
            wifiConfiguration = (WifiConfiguration) method.invoke(manager);
        } catch (NoSuchMethodException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        return wifiConfiguration;

    }

    private static boolean isWifiApActivated(Context context) {
        boolean value = false;
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
            method.setAccessible(true); // in the case of visibility change in
            // future APIs
            value = (Boolean) method.invoke(manager);
        } catch (NoSuchMethodException e) {
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        return value;
    }

    private static String convertInToIpAddress(int ipAddress) {
        byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
        byte[] bytes2 = new byte[4];
        bytes2[0] = bytes[3];
        bytes2[1] = bytes[2];
        bytes2[2] = bytes[1];
        bytes2[3] = bytes[0];
        try {
            InetAddress address = InetAddress.getByAddress(bytes2);
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            return null;
        }
    }

}




Java Source Code List

com.ggt.cardeto.CardetoService.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServerModule.java
com.ggt.cardeto.embeddedwebserver.CardetoWebServer.java
com.ggt.cardeto.embeddedwebserver.appinfomodule.AppInfoModule.java
com.ggt.cardeto.embeddedwebserver.clipboardmodule.ClipboardModule.java
com.ggt.cardeto.embeddedwebserver.logcatmodule.LogcatModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.GenericSQLiteDatabaseHelper.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.SQLiteModule.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.CSVRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.HtmlRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.JsonRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.TableOutputRenderer.java
com.ggt.cardeto.embeddedwebserver.sqlitemodule.tableouputrenderers.XmlRenderer.java
com.ggt.cardeto.embeddedwebserver.staticvarsmodule.StaticVarsModule.java
com.ggt.cardeto.externallib.NanoHTTPD.java
com.ggt.cardeto.utils.CardetoConstants.java
com.ggt.cardeto.utils.NetworkUtilsOld.java
com.ggt.cardeto.utils.NetworkUtils.java
com.ggt.cardetosample.TestActivity.java
com.ggt.cardetosample.database.CardetoSampleDatabaseDataSource.java
com.ggt.cardetosample.database.CardetoSampleDatabaseHelper.java
com.ggt.cardetosample.database.Log.java