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

<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);
}
}
Kotlin Example
package com.code2care.tools.wifidetails
import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
val connInfo: WifiInfo = wifiManager.connectionInfo
val ipAddress = connInfo.ipAddress
val ipAddressValue = String.format("%d.%d.%d.%d",
(ipAddress and 0xff),
(ipAddress shr 8 and 0xff),
(ipAddress shr 16 and 0xff),
(ipAddress shr 24 and 0xff))
val wifiInfo = HashMap()
wifiInfo["SSID"] = connInfo.ssid ?: "N/A"
wifiInfo["IP Address"] = ipAddressValue
wifiInfo["MAC Address"] = connInfo.macAddress ?: "N/A"
wifiInfo["LinkSpeed"] = "${connInfo.linkSpeed} ${WifiInfo.LINK_SPEED_UNITS}"
val data = wifiInfo.entries.joinToString("\n") { "${it.key} : ${it.value}" }
wifiDetails.text = 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.performLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$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(Method.java:507)
at android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!