get WIFI Local Ip Address - Android Wifi

Android examples for Wifi:Wifi Address

Description

get WIFI Local Ip Address

Demo Code


//package com.java2s;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo.State;

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

import android.telephony.TelephonyManager;

public class Main {

    public static String getWIFILocalIpAdress(Context mContext) {

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

        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }//w ww  . j a  va2 s.c o m
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        return formatIpAddress(ipAddress);
    }

    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
                .getActiveNetworkInfo().getState() == State.CONNECTED) || mgrTel
                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    }

    private static String formatIpAddress(int ipAdress) {
        return (ipAdress & 0xFF) + "." + ((ipAdress >> 8) & 0xFF) + "."
                + ((ipAdress >> 16) & 0xFF) + "." + (ipAdress >> 24 & 0xFF);
    }
}

Related Tutorials