Get Wifi Details : Android Programming


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

Wifi Details Android Programming
Wifi Details Android Programming
layout.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>
File : MainActivity.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 networks.
          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 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 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)
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency