get Wifi Ip Address - Android Phone

Android examples for Phone:wifi

Description

get Wifi Ip Address

Demo Code


//package com.java2s;

import android.content.Context;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import android.util.Log;

public class Main {
    private static final String TAG = "DeviceUtil";

    public static String getWifiIpAddress(Context context) {
        String ipAdress = null;/*  ww w.ja v a2 s  .c  o m*/

        WifiManager wifiManager = (WifiManager) context
                .getSystemService(context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        Log.i(TAG, "ipAddress:" + ipAddress);
        int[] ipAddr = new int[4];
        ipAddr[0] = ipAddress & 0xFF;
        ipAddr[1] = (ipAddress >> 8) & 0xFF;
        ipAddr[2] = (ipAddress >> 16) & 0xFF;
        ipAddr[3] = (ipAddress >> 24) & 0xFF;

        ipAdress = new StringBuilder().append(ipAddr[0]).append(".")
                .append(ipAddr[1]).append(".").append(ipAddr[2])
                .append(".").append(ipAddr[3]).append(".").toString();
        Log.i(TAG, "DeviceUtil.getWifiIpAddress.ipAdress:" + ipAdress);

        return ipAdress;

    }
}

Related Tutorials