Get Wifi Details : Android Programming


Posted on : Wed, 15th Apr 15 01:50 pm (Updated on : Wed, 15th Apr 15 01:50 pm) UTC
Author : Code2care
100+ votes

Fig 1 : Wifi Details : Android Programming

Below is an Example of getting Wifi Details like : SSID, IP address, Mac address and Link Speed in Android Programming

layout.xml Language : XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/wifiDetails"
            android:id="@+id/wifiDetails" />
    </LinearLayout>


Advertisements





File : MainActivity.java Language : Java

package com.code2care.tools.wifidetails;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

import com.code2care.tools.detectphoneshakes.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class MainActivity extends ActionBarActivity  {

   private TextView wifiDetails;
 private String data;
   private String ipAddressValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        wifiDetails = (TextView)findViewById(R.id.wifiDetails);

        data ="";

        WifiManager mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiInfo connInfo = mWifiManager.getConnectionInfo();


        //Get IP Address
        int ipAddress = connInfo.getIpAddress();


        //Converting IP address from hex to decimal
         ipAddressValue = String.format("%d.%d.%d.%d",
                (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff),
                (ipAddress >> 24 & 0xff));

        final int NumOfRSSILevels = 5;

        HashMap<String,String> wifiInfo = new HashMap<String,String>();


        /* Returns the service set identifier (SSID) of the current 802.11 network.
          If the SSID can be decoded as UTF-8, it will be returned surrounded by double quotation marks.
          Otherwise, it is returned as a string of hex digits.
          The SSID may be null if there is no network currently connected. */

        wifiInfo.put("SSID",connInfo.getSSID());



        wifiInfo.put("IP Address",ipAddressValue+"");
        wifiInfo.put("MAC Address",connInfo.getMacAddress());
        wifiInfo.put("LinkSpeed",connInfo.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS);




        Iterator it = wifiInfo.entrySet().iterator();

        while (it.hasNext()) {

            Map.Entry pair = (Map.Entry)it.next();

            data = data+pair.getKey() + " : " + pair.getValue()+"\n";

            it.remove();
        }


        wifiDetails.setText(data);



    }



}

Note : Do not forget to add user permission for <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> or else you make get the below error in console,

04-03 17:46:56.209      603-603/com.code2care.tools.detectphoneshakes E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.code2care.tools.wifidetection/com.code2care.tools.wifidetection.MainActivity}:
    java.lang.SecurityException: WifiService: Neither user 10101 nor current process has android.permission.ACCESS_WIFI_STATE.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.SecurityException: WifiService: Neither user 10101 nor current process has android.permission.ACCESS_WIFI_STATE.
            at android.os.Parcel.readException(Parcel.java:1322)
            at android.os.Parcel.readException(Parcel.java:1276)
            at android.net.wifi.IWifiManager$Stub$Proxy.getConnectionInfo(IWifiManager.java:1035)
            at android.net.wifi.WifiManager.getConnectionInfo(WifiManager.java:740)
            at com.code2care.tools.detectphoneshakes.MainActivity.onCreate(MainActivity.java:32)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
            at dalvik.system.NativeStart.main(Native Method)

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +