Android to get the current connected WiFi signal strength method _android

Source: Internet
Author: User

This article illustrates how Android gets the strength of the currently connected WiFi signal, and is a very common and important skill in Android development. Share for everyone to use for reference. The specific methods are as follows:

1. Get WiFi information that is currently connected

Wifimanager Wifi_service = (wifimanager) getsystemservice (wifi_service); 
Wifiinfo wifiinfo = Wifi_service.getconnectioninfo ();

The Wifiinfo has the following methods:

Wifiinfo.getbssid ();
Wifiinfo.getssid ();
Wifiinfo.getipaddress (); Gets the IP address.
Wifiinfo.getmacaddress (); Get MAC address.
Wifiinfo.getnetworkid (); Get the network ID.
Wifiinfo.getlinkspeed (); Gets the connection speed that allows the user to learn about this information.

Wifiinfo.getrssi (); Get Rssi,rssi is to accept signal strength indication. this can be directly compared to Huawei's Wi-Fi signal thresholds to provide users with a network or geographic adjustment to achieve the best connection effect.
here the signal strength is obtained by Wifiinfo.getrssi ();

2. The resulting value is an interval value of 0 to 100, which is an int, where 0 to 50 indicates the best signal, and-50 to 70 indicates a signal deviation, less than-70 indicates the worst, and may not connect or fall off the line.
What I do here is to change the picture based on the signal strength. The settings configuration file Wifi_sel.xml as follows:

<level-list xmlns:android= "Http://schemas.android.com/apk/res/android" >  
  <item android:maxlevel= "50" android:drawable= "@drawable/library_template_05"/>  
  <item android:maxlevel= "android:drawable=" @ Drawable/library_template_05_2 "/>  
  <item android:maxlevel=" android:drawable= "@drawable/library_ Template_05_3 "/>  
</level-list> 

Note that this is all absolute, because at imageview.setimagelevel level, the level must be absolute, otherwise the program will report a null pointer.

3. Registering for listening, and Android battery listening display similar

WiFi-related  
intentfilter wifiintentfilter;  WiFi Listener 

In the OnCreate method, add:

WiFi  
wifiintentfilter = new Intentfilter ();  
Wifiintentfilter.addaction (wifimanager.wifi_state_changed_action); 

4. Then again:

Declare wifi message processing private broadcastreceiver Wifiintentreceiver = new Broadcastreceiver () {@Override public void onre  
    Ceive (context context, Intent Intent) {int wifi_state = Intent.getintextra ("Wifi_state", 0); 
    int level = Math.Abs ((wifimanager) Getsystemservice (Wifi_service)). Getconnectioninfo (). Getrssi ());  
    LOG.I (Global.tag, "1111:" + level); Switch (wifi_state) {case wifimanager.wifi_state_disabling:log.i (Global.tag, "1111:" + wifimanager.wifi_st  
      ate_disabling);  
      Wifi_image.setimageresource (R.drawable.wifi_sel);  
      Wifi_image.setimagelevel (level);  
    Break  
      Case WIFIMANAGER.WIFI_STATE_DISABLED:LOG.I (Global.tag, "2222:" + wifimanager.wifi_state_disabled);  
      Wifi_image.setimageresource (R.drawable.wifi_sel);  
      Wifi_image.setimagelevel (level);  
    Break  
      Case WifiManager.WIFI_STATE_ENABLING:wifi_image.setImageResource (R.drawable.wifi_sel);  
 Wifi_image.setimagelevel (level);     LOG.I (Global.tag, "33333:" + wifimanager.wifi_state_enabling);  
    Break  
      Case WIFIMANAGER.WIFI_STATE_ENABLED:LOG.I (Global.tag, "4444:" + wifimanager.wifi_state_enabled);  
      Wifi_image.setimageresource (R.drawable.wifi_sel);  
      Wifi_image.setimagelevel (level);  
    Break  
      Case WIFIMANAGER.WIFI_STATE_UNKNOWN:LOG.I (Global.tag, "5555:" + wifimanager.wifi_state_unknown);  
      Wifi_image.setimageresource (R.drawable.wifi_sel);  
      Wifi_image.setimagelevel (level);  
    Break 

 }  
  }  
};

5. Registered in the Onresume method, destroyed in the OnPause method :

@Override  
protected void Onresume () {  
  super.onresume ();
  Register WiFi Message Processor  
  registerreceiver (Wifiintentreceiver, wifiintentfilter); 
@Override  
protected void OnPause () {  
  super.onpause ();  
  Unregisterreceiver (Wifiintentreceiver);  
} 

6. Finally add permission:

<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>

It is believed that this article has certain reference value to everyone's Android program design.

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.