Android WIFI Information Retrieval

Source: Internet
Author: User
Tags bssid

You can obtain WIFI information in androi through the Wi-Fi Service provided by the system.

[Java]
WifiManager wifi_service = (WifiManager) getSystemService (WIFI_SERVICE );
WifiInfo wifiInfo = wifi_service.getConnectionInfo ();

Among them, WifiInfo has the following common information:
[Java]
/*
Info. getBSSID (); obtain the BSSID address.
Info. getSSID (); get the SSID address. ID of the network to be connected
Info. getIpAddress (); obtain the IP address. 4-byte Int, XXX. XXX every XXX is a byte
Info. getMacAddress (); obtain the MAC address.
Info. getNetworkId (); obtain the network ID.
Info. getLinkSpeed (); get the connection speed to let the user know this information.
Info. getreceipt (); obtain the receipt of the signal strength indication.
*/

At the same time, you can obtain the current WIFI status by receiving system broadcasts.
[Java]
/*
WifiManager. WIFI_STATE_DISABLING is stopping
WifiManager. WIFI_STATE_DISABLED stopped
WifiManager. WIFI_STATE_ENABLING is enabling
WifiManager. WIFI_STATE_ENABLED Enabled
WifiManager. WIFI_STATE_UNKNOWN unknown
*/

The following are examples:
1. Add a WIFI icon and text (activity_wifi_example.xml) to the layout file)
[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 for the WIFI icon and add the ic_wifi.xml file under the drawable-hdpi directory. The content is as follows:
[Html] view plaincopy
<Level-list xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: maxLevel = "0" android: drawable = "@ drawable/ic_wifi_s0"/>
<Item android: maxLevel = "50" android: drawable = "@ drawable/ic_wifi_s3"/>
<Item android: maxLevel = "70" android: drawable = "@ drawable/ic_wifi_s2"/>
<Item android: maxLevel = "100" android: drawable = "@ drawable/ic_wifi_s1"/>
</Level-list>
Note the image to be imported

3. Receive the WIFI status broadcast in the Code and enable a Ms Timer to query the WIFI status in real time (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. getarg ()));

/*
Info. getBSSID (); obtain the BSSID address.
Info. getSSID (); get the SSID address. ID of the network to be connected
Info. getIpAddress (); obtain the IP address. 4-byte Int, XXX. XXX every XXX is a byte
Info. getMacAddress (); obtain the MAC address.
Info. getNetworkId (); obtain the network ID.
Info. getLinkSpeed (); get the connection speed to let the user know this information.
Info. getreceipt (); obtain the receipt of the signal strength indication.
*/

Int Ip = info. getIpAddress ();
String strIp = "" + (Ip & 0xFF) + ". "+ (Ip> 8) & 0xFF) + ". "+ (Ip> 16) & 0xFF) + ". "+ (Ip> 24) & 0xFF );

MLabelWifi. setText ("BSSID:" + info. getBSSID () + "\ nSSID:" + info. getSSID () +
"\ NIpAddress:" + strIp + "\ nMacAddress:" + info. getMacAddress () +
"\ NNetworkId:" + info. getNetworkId () + "\ nLinkSpeed:" + info. getLinkSpeed () + "Mbps" +
"\ NRssi:" + info. getarg ());
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. getarg ()));

/*
WifiManager. WIFI_STATE_DISABLING is stopping
WifiManager. WIFI_STATE_DISABLED stopped
WifiManager. WIFI_STATE_ENABLING is enabling
WifiManager. WIFI_STATE_ENABLED Enabled
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. Added support for obtaining WIFI permissions in AndroidManifest. xml.
[Html]
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>


 

Related Article

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.