Below is an Example of getting Wifi Details like: SSID, IP address, Mac address and Link Speed in 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)
More Posts related to Android,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- How to remove username from Mac Menu Bar? - MacOS
- Setting JAVA_HOME in macOS Big Sur - MacOS
- TextEdit Get the count of lines in a file - MacOS
- Fix: Microsoft Teams Error Code 2:- 1200 - Teams
- Make Android View Scrollable both Horizontally and Vertically - Android
- Fix Microsoft Teams Error code - 107 - Teams
- How to run Java Unit Test cases with Apache Maven? - Java
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle. - Java