Example usage for android.net.wifi WifiInfo getIpAddress

List of usage examples for android.net.wifi WifiInfo getIpAddress

Introduction

In this page you can find the example usage for android.net.wifi WifiInfo getIpAddress.

Prototype

public int getIpAddress() 

Source Link

Usage

From source file:Main.java

public static String getWifiIpAddress(Context pContext) {
    WifiInfo localWifiInfo = null;
    if (pContext != null) {
        localWifiInfo = ((WifiManager) pContext.getSystemService("wifi")).getConnectionInfo();
        if (localWifiInfo != null) {
            String str = convertIntToIp(localWifiInfo.getIpAddress());
            return str;
        }//from  w ww.j  a  v  a2s . c  o m
    }
    return "";
}

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;//www.  jav a2 s. com

    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:com.doomy.padlock.MainActivity.java

/**
 * Gets the ip adress wireless.//from w  ww .j  a  v  a 2 s  .co 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;
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonWifiInfo(WifiInfo data) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("hidden_ssid", data.getHiddenSSID());
    result.put("ip_address", data.getIpAddress());
    result.put("link_speed", data.getLinkSpeed());
    result.put("network_id", data.getNetworkId());
    result.put("rssi", data.getRssi());
    result.put("bssid", data.getBSSID());
    result.put("mac_address", data.getMacAddress());
    result.put("ssid", data.getSSID());
    String supplicantState = "";
    switch (data.getSupplicantState()) {
    case ASSOCIATED:
        supplicantState = "associated";
        break;/*  w w w.  j  av  a 2 s. c om*/
    case ASSOCIATING:
        supplicantState = "associating";
        break;
    case COMPLETED:
        supplicantState = "completed";
        break;
    case DISCONNECTED:
        supplicantState = "disconnected";
        break;
    case DORMANT:
        supplicantState = "dormant";
        break;
    case FOUR_WAY_HANDSHAKE:
        supplicantState = "four_way_handshake";
        break;
    case GROUP_HANDSHAKE:
        supplicantState = "group_handshake";
        break;
    case INACTIVE:
        supplicantState = "inactive";
        break;
    case INVALID:
        supplicantState = "invalid";
        break;
    case SCANNING:
        supplicantState = "scanning";
        break;
    case UNINITIALIZED:
        supplicantState = "uninitialized";
        break;
    default:
        supplicantState = null;
    }
    result.put("supplicant_state", build(supplicantState));
    return result;
}

From source file:org.basdroid.common.NetworkUtils.java

public static String getMacAddressFromNetworkInterface(final Context context) {

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();

    // Convert little-endian to big-endianif needed
    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }//w ww  . ja  v a 2s.  c  o  m

    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();

    String result;
    try {
        InetAddress addr = InetAddress.getByAddress(bytes);
        NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr);
        Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName());

        byte[] mac = netInterface.getHardwareAddress();
        if (mac == null || mac.length == 0)
            return "";
        StringBuilder buf = new StringBuilder();
        for (int idx = 0; idx < mac.length; idx++) {
            buf.append(String.format("%02X:", mac[idx]));
        }
        if (buf.length() > 0)
            buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
    } catch (UnknownHostException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex);
        result = null;
    } catch (SocketException ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex);
        result = null;
    } catch (Exception ex) {
        Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex);
        result = null;
    }

    return result;
}

From source file:com.wso2.mobile.mdm.api.PhoneState.java

/**
*Returns the device IP address/*from w w w . ja  v  a2  s .c  om*/
*/
public String getIpAddress() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    String ip = intToIp(ipAddress);
    return ip;
}

From source file:org.peterbaldwin.vlcremote.app.PickServerActivity.java

private byte[] getIpAddress() {
    WifiInfo info = getConnectionInfo();
    if (info != null) {
        return toByteArray(info.getIpAddress());
    }
    return null;
}

From source file:de.taxilof.UulmLoginAgent.java

/**
 * fetch the IP of the Device/*  w w  w  .j  a v  a  2 s .  c o  m*/
 * 
 * @return ip
 */
private String getIp() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ipAddress = null;
    if (wifiInfo != null) {
        long addr = wifiInfo.getIpAddress();
        if (addr != 0) {
            if (addr < 0)
                addr += 0x100000000L; // handle negative values whe first
            // octet > 127
            ipAddress = String.format("%d.%d.%d.%d", addr & 0xFF, (addr >> 8) & 0xFF, (addr >> 16) & 0xFF,
                    (addr >> 24) & 0xFF);
        }
    }
    return ipAddress;
}

From source file:com.iiitd.networking.UDPMessenger.java

/**
 * Sends a broadcast message (TAG EPOCH_TIME message). Opens a new socket in case it's closed.
 * @param message the message to send (multicast). It can't be null or 0-characters long.
 * @return/*from  w  ww . j a  v  a 2  s.c o m*/
 * @throws IllegalArgumentException
 */
public boolean sendMessage(String message) throws IllegalArgumentException {
    if (message == null || message.length() == 0)
        throw new IllegalArgumentException();

    // Check for WiFi connectivity
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (mWifi == null || !mWifi.isConnected()) {
        Log.d(DEBUG_TAG,
                "Sorry! You need to be in a WiFi network in order to send UDP multicast packets. Aborting.");
        return false;
    }

    // Check for IP address
    WifiManager wim = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int ip = wim.getConnectionInfo().getIpAddress();

    // Create the send socket
    if (socket == null) {
        try {
            socket = new DatagramSocket();
        } catch (SocketException e) {
            Log.d(DEBUG_TAG, "There was a problem creating the sending socket. Aborting.");
            e.printStackTrace();
            return false;
        }
    }

    // Build the packet
    DatagramPacket packet;
    Message msg = new Message(TAG, message);
    byte data[] = msg.toString().getBytes();

    WifiInfo wifiInfo = wim.getConnectionInfo();
    int ipa = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ipa);

    try {
        //         packet = new DatagramPacket(data, data.length, InetAddress.getByName(ipToString(ip, true)), MULTICAST_PORT);
        packet = new DatagramPacket(data, data.length, InetAddress.getByName(ipAddress), MULTICAST_PORT);
    } catch (UnknownHostException e) {
        Log.d(DEBUG_TAG, "It seems that " + ipToString(ip, true) + " is not a valid ip! Aborting.");
        e.printStackTrace();
        return false;
    }

    try {
        socket.send(packet);
    } catch (IOException e) {
        Log.d(DEBUG_TAG, "There was an error sending the UDP packet. Aborted.");
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.fastbootmobile.encore.cast.CastModule.java

private String getWiFiIpAddress() {
    WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return Formatter.formatIpAddress(ip);
}