"Android" gets the signal strength of the currently connected WiFi

Source: Internet
Author: User

1. Get the currently connected WiFi information

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

Where Wifiinfo has the following methods:

Wifiinfo.getbssid ();

Wifiinfo.getssid ();

Wifiinfo.getipaddress (); Gets the IP address.

Wifiinfo.getmacaddress (); Gets the MAC address.

Wifiinfo.getnetworkid (); Gets the network ID.

Wifiinfo.getlinkspeed (); Gets the connection speed, which allows the user to learn about this information.

Wifiinfo.getrssi (); getting rssi,rssi is the signal strength indication. This can be compared directly to the Wi-Fi signal thresholds provided by Huawei to provide users with the ability to make adjustments to the network or geographic location for the best connectivity.

Here the signal strength depends on Wifiinfo.getrssi (); this method.

2. The obtained value is a 0 to 100 interval value, is an int data, where 0 to 50 indicates the best signal, 50 to 70 indicates a signal deviation, less than 70 is the worst, it is possible to connect or drop the line.

What I do here is to change the image according to the signal strength. Set the 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 an absolute value, because at imageview.setimagelevel level, the level must be an absolute value, or the program will report a null pointer.

3. Register 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:

    Claims WiFi message processing private broadcastreceiver Wifiintentreceiver = new Broadcastreceiver () {@Override              public void OnReceive (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:" + Wifim Anager.                 wifi_state_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. Register in the Onresume method and destroy it 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. Last Added permission:

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

"Android" gets the signal strength of the currently connected WiFi

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.