Android WiFi Info get

Source: Internet
Author: User

Access to WiFi information in Androi can be obtained via the WiFi service provided by the system

[Java]
Wifimanager Wifi_service = (wifimanager) getsystemservice (Wifi_service);
Wifiinfo wifiinfo = Wifi_service.getconnectioninfo ();

The more commonly used information in Wifiinfo is:
[java] 
 /*
   info.getbssid ();       get BSSID address.
   info.getssid ();       gets the SSID address.   The ID
   info.getipaddress ();  need to connect to the network to get the IP address. 4 byte int, XXX.XXX.XXX.XXX each XXX as a byte
   info.getmacaddress (); Get MAC address.
   info.getnetworkid ();  get the network ID.
   info.getlinkspeed ();  Gets the connection speed, which allows the user to learn about this information.
   Info.getrssi ();       get Rssi,rssi is to receive signal strength indication
*/ 

The status of the current WiFi can be obtained by receiving the broadcast of the system.
[Java]
/*
Wifimanager.wifi_state_disabling is stopping.
Wifimanager.wifi_state_disabled has stopped
Wifimanager.wifi_state_enabling is opening.
Wifimanager.wifi_state_enabled is open
Wifimanager.wifi_state_unknown Unknown
*/

Specific examples are given below:
1. Add a WiFi icon to the layout file, and text that displays specific information (activity_wifi_example.xml)
[HTML]
<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:gravity= "Center"
android:orientation= "Vertical" >

<imagebutton
Android:id= "@+id/icon_wifistatus"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_wifi"/>

<textview
Android:id= "@+id/label_wifidetail"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:maxwidth= "320DP"/>

</LinearLayout>

2. Add an optional set of icons to the WiFi icon, and add the Ic_wifi.xml file to the drawable-hdpi directory as follows:
[HTML] View plaincopy
<level-list xmlns:android= "Http://schemas.android.com/apk/res/android" >&NBSP;
    <item android:maxlevel= "0"     android:drawable= "@drawable/ic_wifi_s0"/>&NBSP;
    <item android:maxlevel= "50"   android:drawable= "@drawable/ic_wifi_s3"/>&NBSP;
    <item android:maxlevel= "70"   android:drawable= "@drawable/ic_wifi_s2"/>&NBSP;
    <item android:maxlevel= "100" android:drawable= "@drawable/ic_wifi_s1"/>&NBSP;
</level-list> 
Note the image you need to import is

3. Receive WiFi status broadcast in code, turn on a 500ms timer real-time query WiFi status (Wifiexample.java)

[Java]
Package com.example.wifiexample;

Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.net.wifi.WifiInfo;
Import Android.net.wifi.WifiManager;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.widget.ImageButton;
Import Android.widget.TextView;

public class Wifiexample extends Activity {

Private final String TAG = "Wifiexample";

Private Intentfilter Mwifiintentfilter;
Private Broadcastreceiver Mwifiintentreceiver;

Private ImageButton Miconwifi;
Private TextView Mlabelwifi;
Private Handler Mhandler;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_wifi_example);

Miconwifi = (ImageButton) Findviewbyid (r.id.icon_wifistatus);
Mlabelwifi = (TextView) Findviewbyid (R.id.label_wifidetail);

Mwifiintentfilter = new Intentfilter ();
Mwifiintentfilter.addaction (wifimanager.wifi_state_changed_action);

Mwifiintentreceiver = new Mwifiintentreceiver ();
Registerreceiver (Mwifiintentreceiver, Mwifiintentfilter);

Mhandler = new Handler ();
Mhandler.post (New timerprocess ());
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (r.menu.activity_wifi_example, menu);
return true;
}

Private class Timerprocess implements runnable{
public void Run () {
Showwifidetail ();
Mhandler.postdelayed (this, 500);
}
}

public void Showwifidetail ()
{
Wifiinfo info = ((wifimanager) Getsystemservice (Wifi_service)). Getconnectioninfo ();
Miconwifi.setimagelevel (Math.Abs (Info.getrssi ()));

/*
Info.getbssid (); Gets the BSSID address.
Info.getssid (); Gets the SSID address. ID that needs to connect to the network
Info.getipaddress (); Gets the IP address. 4 byte int, XXX.XXX.XXX.XXX per XXX for one byte
Info.getmacaddress (); Gets the MAC address.
Info.getnetworkid (); Gets the network ID.
Info.getlinkspeed (); Gets the connection speed, which allows the user to learn about this information.
Info.getrssi (); Get Rssi,rssi is the signal strength indication
*/

int Ip = info.getipaddress ();
String strIp = "" + (IP & 0xFF) + "." + ((IP >> 8) & 0xFF) + "." + ((IP >>) & 0xFF) + "." + ((I P >>) & 0xFF);

Mlabelwifi.settext ("BSSID:" + info.getbssid () + "\nssid:" + info.getssid () +
"\nipaddress:" + strIp + "\nmacaddress:" + info.getmacaddress () +
"\nnetworkid:" + info.getnetworkid () + "\nlinkspeed:" + info.getlinkspeed () + "Mbps" +
"\nrssi:" + Info.getrssi ());
Info.getipaddress ();
}

Private class Mwifiintentreceiver extends broadcastreceiver{

public void OnReceive (context context, Intent Intent) {

Wifiinfo info = ((wifimanager) Getsystemservice (Wifi_service)). Getconnectioninfo ();
Miconwifi.setimagelevel (Math.Abs (Info.getrssi ()));

/*
Wifimanager.wifi_state_disabling is stopping.
Wifimanager.wifi_state_disabled has stopped
Wifimanager.wifi_state_enabling is opening.
Wifimanager.wifi_state_enabled is open
Wifimanager.wifi_state_unknown Unknown
*/

Switch (Intent.getintextra ("Wifi_state", 0)) {
Case wifimanager.wifi_state_disabling:
LOG.D (TAG, "WIFI status:wifi_state_disabling");
Break
Case wifimanager.wifi_state_disabled:
LOG.D (TAG, "WIFI status:wifi_state_disabled");
Break
Case wifimanager.wifi_state_enabling:
LOG.D (TAG, "WIFI status:wifi_state_enabling");
Break
Case wifimanager.wifi_state_enabled:
LOG.D (TAG, "WIFI status:wifi_state_enabled");
Break
Case Wifimanager.wifi_state_unknown:
LOG.D (TAG, "WIFI status:wifi_state_unknown");
Break
}
}
}
}

4. Finally added support for getting WiFi permissions in Androidmanifest.xml
[HTML]
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android WiFi Info get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.