Example usage for android.net.wifi WifiManager getConnectionInfo

List of usage examples for android.net.wifi WifiManager getConnectionInfo

Introduction

In this page you can find the example usage for android.net.wifi WifiManager getConnectionInfo.

Prototype

public WifiInfo getConnectionInfo() 

Source Link

Document

Return dynamic information about the current Wi-Fi connection, if any is active.

Usage

From source file:Main.java

public static String getWifiIp(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    boolean enable = wifiManager.isWifiEnabled();
    if (!enable) {
        return null;
    }//from   w w  w. j  av a2 s  .  co  m
    WifiInfo wifiinfo = wifiManager.getConnectionInfo();
    return intToIp(wifiinfo.getIpAddress());
}

From source file:eu.vranckaert.worktime.utils.network.NetworkUtil.java

/**
 * Checks if the device is connected to a WiFi network or not.
 * @param ctx The app-context.//  w  ww.j  a  v a  2s .  c  o m
 * @return {@link Boolean#TRUE} if the device is connected to a WiFi network, {@link Boolean#FALSE} otherwise.
 */
public static boolean isConnectedToWifi(Context ctx) {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        SupplicantState state = wifiManager.getConnectionInfo().getSupplicantState();
        if (state != null) {
            NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf(state);
            if (detailedState != null) {
                if (detailedState == NetworkInfo.DetailedState.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

@SuppressLint("HardwareIds")
static String getPhoneMacAddress(Context context) {
    String mac = "";

    InputStreamReader inputStreamReader = null;
    LineNumberReader lineNumberReader = null;
    try {/*from www .  java  2  s  .  c om*/
        @SuppressLint("WifiManagerPotentialLeak")
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        mac = info.getMacAddress();

        if (TextUtils.isEmpty(mac)) {
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            inputStreamReader = new InputStreamReader(process.getInputStream());
            lineNumberReader = new LineNumberReader(inputStreamReader);
            String line = lineNumberReader.readLine();
            if (line != null) {
                mac = line.trim();
            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (lineNumberReader != null) {
            try {
                lineNumberReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    if (TextUtils.isEmpty(mac)) {
        mac = "na";
    }

    return mac;
}

From source file:com.vinexs.tool.NetworkManager.java

public static String getSSID(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    return info.getBSSID();
}

From source file:com.vinexs.tool.NetworkManager.java

public static String getMacAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    return wInfo.getMacAddress();
}

From source file:org.wahtod.wififixer.ui.StatusFragment.java

private static WifiInfo getNetwork(Context context) {
    WifiManager wm = AsyncWifiManager.getWifiManager(context);
    if (wm.isWifiEnabled()) {
        return wm.getConnectionInfo();
    } else//  w w  w .j ava2  s  .c o m
        return null;
}

From source file:org.droidupnp.Main.java

public static InetAddress getLocalIpAddress(Context ctx) throws UnknownHostException {
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    if (ipAddress != 0)
        return InetAddress.getByName(String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)));

    Log.d(TAG, "No ip adress available throught wifi manager, try to get it manually");

    InetAddress inetAddress;/*from ww w  . j a v  a2  s  .  c om*/

    inetAddress = getLocalIpAdressFromIntf("wlan0");
    if (inetAddress != null) {
        Log.d(TAG, "Got an ip for interfarce wlan0");
        return inetAddress;
    }

    inetAddress = getLocalIpAdressFromIntf("usb0");
    if (inetAddress != null) {
        Log.d(TAG, "Got an ip for interfarce usb0");
        return inetAddress;
    }

    return InetAddress.getByName("0.0.0.0");
}

From source file:Main.java

/**
 * @Thach @SK29/*from ww w.  j a  v a2s . c o m*/
 * @Init MAC address and save to preference
 * @param context
 * @return
 */
private static String initMacAddress(Context context) {
    // INIT macAddress
    String macAddress;

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

    } else {
        // ENABLE THE WIFI FIRST
        wifiManager.setWifiEnabled(true);

        // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

        // NOW DISABLE IT AGAIN
        wifiManager.setWifiEnabled(false);
    }

    if (macAddress == null) {
        macAddress = android.os.Build.ID;
    }

    return macAddress;
}

From source file:org.cocos2dx.lib.CCUtils.java

public static String getMacAddress() {
    String mac = "";
    Context ctx = Cocos2dxActivity.getContext();

    // first, try to get mac from wifi manager
    if (ctx.checkCallingPermission(permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();//from  w  w  w .  j  a va  2  s  .c o  m
    }

    // if failed, try from network interface api
    if (TextUtils.isEmpty(mac)) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                NetworkInterface ne = NetworkInterface
                        .getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                byte[] b = ne.getHardwareAddress();
                mac = byte2Hex(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // if failed, use fake
    if (TextUtils.isEmpty(mac)) {
        mac = "00:00:00:00:00:00";
    }

    // return
    return mac;
}

From source file:com.doomy.padlock.MainActivity.java

/**
 * Gets the ip adress wireless.//from  w  ww  .j a v  a  2 s .c o m
 *
 * @return The ip adress wireless.
 */
public static String getIPAdressWifi() {
    WifiManager mWifiManager = (WifiManager) mActivity.getSystemService(WIFI_SERVICE);
    WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
    int mIP = mWifiInfo.getIpAddress();
    String mIPAddress = Formatter.formatIpAddress(mIP);
    return mIPAddress;
}