Below is an Example of getting Wifi Details like: SSID, IP address, Mac address and Link Speed in 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,
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
More Posts:
- Add Text at Start and End of Each Line Notepad++ - NotepadPlusPlus
- How to Comment out Code in PowerShell Script - Powershell
- How to make a Android button act as a toggle button - Android
- How to add hours and minutes to Java Instant - Java
- PHP 301 Redirect Permanently - PHP
- Round Number up to 2 decimal places in Python - Python
- Accept Only 0-9 Numbers RegEx Example - HowTos
- Take input argument from command line in Python Programming - Python