SoFunction
Updated on 2025-03-11

How to get the currently connected wifi signal strength of Android

This article describes the method of obtaining the currently connected wifi signal strength of Android, which is a very common and important technique in Android program development. Share it for your reference. The specific methods are as follows:

1. Get the currently connected wifi information

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

Among them, wifiInfo has the following methods:

();
();
(); Get the IP address.
(); Get the MAC address.
(); Obtain the network ID.
(); Obtaining the connection speed allows users to know this information.

(); Obtain RSSI, RSSI is to accept signal strength indication.This can be directly compared with the Wi-Fi signal threshold provided by Huawei to provide users with, allowing users to make adjustments to the network or geographical location to obtain the best connection effect.
The signal strength is obtained here by (); this method.
 
2. The obtained value is an interval value of 0 to -100, which is an int type data. 0 to -50 means the signal is the best, -50 to -70 means the signal deviation, and less than -70 means the worst, and it may not be connected or disconnected.
What I did here is to change the picture according to the signal strength. Set the configuration file wifi_sel.xml as follows:

<level-list xmlns:andro>  
  <item android:maxLevel="50" android:drawable="@drawable/library_template_05" />  
  <item android:maxLevel="70" android:drawable="@drawable/library_template_05_2" />  
  <item android:maxLevel="100" android:drawable="@drawable/library_template_05_3" />  
</level-list> 

Note that all of these are absolute values, because at level, level must be absolute values, otherwise the program will report a null pointer.

3. Register listening, similar to Android battery listening

// Wifi relatedIntentFilter wifiIntentFilter;  // Wifi listener

In the oncreate method:

// wifi  
wifiIntentFilter = new IntentFilter();  
(WifiManager.WIFI_STATE_CHANGED_ACTION); 

4. Then:

// Declare the wifi message processing processprivate BroadcastReceiver wifiIntentReceiver = new BroadcastReceiver() {  
@Override  
public void onReceive(Context context, Intent intent) {  
    int wifi_state = ("wifi_state", 0);  
    int level = (((WifiManager)getSystemService(WIFI_SERVICE)).getConnectionInfo().getRssi()); 
    (, "1111:" + level);  
    switch (wifi_state) {  
    case WifiManager.WIFI_STATE_DISABLING:  
      (, "1111:" + WifiManager.WIFI_STATE_DISABLING);  
      wifi_image.setImageResource(.wifi_sel);  
      wifi_image.setImageLevel(level);  
      break;  
    case WifiManager.WIFI_STATE_DISABLED:  
      (, "2222:" + WifiManager.WIFI_STATE_DISABLED);  
      wifi_image.setImageResource(.wifi_sel);  
      wifi_image.setImageLevel(level);  
      break;  
    case WifiManager.WIFI_STATE_ENABLING:  
      wifi_image.setImageResource(.wifi_sel);  
      wifi_image.setImageLevel(level);  
      (, "33333:" + WifiManager.WIFI_STATE_ENABLING);  
      break;  
    case WifiManager.WIFI_STATE_ENABLED:  
      (, "4444:" + WifiManager.WIFI_STATE_ENABLED);  
      wifi_image.setImageResource(.wifi_sel);  
      wifi_image.setImageLevel(level);  
      break;  
    case WifiManager.WIFI_STATE_UNKNOWN:  
      (, "5555:" + WifiManager.WIFI_STATE_UNKNOWN);  
      wifi_image.setImageResource(.wifi_sel);  
      wifi_image.setImageLevel(level);  
      break;  
    }  
  }  
}; 

5. Register in the onResume method and destroy in the onPause method.

@Override  
protected void onResume() {  
  ();
  // Register a wifi message processor  registerReceiver(wifiIntentReceiver, wifiIntentFilter);  
} 
@Override  
protected void onPause() {  
  ();  
  unregisterReceiver(wifiIntentReceiver);  
} 

6. Last join permission:

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

I believe that the description in this article has certain reference value for everyone's Android programming design.