Android method of obtaining the signal strength of the currently connected wifi
- 2020-06-07 05:14:57
- OfStack
This article illustrates Android's method of obtaining the signal strength of currently connected wifi, which is a very common and important technique in Android program development. To share with you for your reference. The specific methods are as follows:
1. Get the current connected wifi information
WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifi_service.getConnectionInfo();
wifiInfo has the following methods:
wifiinfo. getBSSID ();
wifiinfo. getSSID ();
wifiinfo. getIpAddress (); Get the IP address.
wifiinfo. getMacAddress (); Get the MAC address.
wifiinfo. getNetworkId (); Get the network ID.
wifiinfo. getLinkSpeed (); Gets the connection speed, which lets the user know this 1 information.
wifiinfo. getRssi (); To get RSSI, RSSI is to receive the signal strength indication. This can be directly compared with the ES39en-ES40en signal threshold provided by Huawei to provide users with the best connection effect by making adjustments to the network or geographical location.
Here the signal strength is obtained by wifiinfo.getRssi (); This method.
2. The obtained value is an interval value from 0 to -100, which is an int type data, in which 0 to -50 means the signal is the best, -50 to -70 means the signal deviation, and less than -70 means the signal deviation, which may be disconnected or dropped.
What I'm doing here is changing the picture based on the signal strength. Set the configuration file ES48en_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="70" android:drawable="@drawable/library_template_05_2" />
<item android:maxLevel="100" android:drawable="@drawable/library_template_05_3" />
</level-list>
Note that these are absolute values, because at imageView.setImageLevel (level), level must be absolute, otherwise the program will report a null pointer.
3. Register for listening, similar to the android battery listening display
// wifi related
IntentFilter wifiIntentFilter; // wifi The listener
Add to the oncreate method:
// wifi
wifiIntentFilter = new IntentFilter();
wifiIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
4. Then:
// The statement 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:" + WifiManager.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 onResume method and destroy in onPause method:
@Override
protected void onResume() {
super.onResume();
// registered wifi Message handler
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 what is described in this paper is of certain reference value to the Android programming.