wifi status monitoring in Android devices

Keywords: Android network Mobile Mac

I. objectives

The APP monitors the status information of wifi connected to Android devices (mobile phones, TV or other devices) and uploads it to the server regularly to analyze the status and signal strength of wifi in the environment where the devices are located at a specific time point.

2, Use scenario

In the actual scene composed of multiple intelligent WiFi devices, the device occasionally fails to control. It is necessary to analyze the reasons for the failure of device control based on the current WiFi state and signal strength in the scene. Therefore, I hope to install this APP on an Android device in the scene, monitor the WiFi status information and upload it to the server.

Premise: the Android device with this APP is connected to the same router with other WiFi devices in the scene.

3, Options

Scheme 1: judge signal strength according to connected wifi information

According to the connected wifi information provided by Android system to developers, the signal strength of wifi environment can be measured. The description and description of relevant parameters (Methods of wifi info class) are as follows:

parameter Parameter (method) describe Explain
mac getBSSID() Gets the current access point for the basic service set identifier (BSSID)
wifi_name getSSID() Get the name of wifi
rssi getRssi() Obtain the strength of wifi received signal Unit: dBm, value range - 100 ~ 0, smaller absolute value, stronger signal
ip getIpAddress() Get the IP address of wifi IP of the connected route
link_speed getLinkSpeed() Get current link speed
frequency getFrequency() Get current frequency
channel getFrequency() channel Current channel can be judged by frequency

In addition, the function of channel can refer to the following link for details:

Among them, the strength of wifi signal is mainly measured according to the value returned by getRssi(), - 50dBm – 0 means the signal is good, - 70dBm~-50dBm means the signal is poor, less than - 70dBm means the signal range is poor, and there is a possibility that there is no connection.
However, the value returned by getRssi() method can only be used to measure the signal strength of the device connected to the router's internal network. As for whether the device can access the external network and the speed of accessing the external network, a new scheme is needed to determine the signal strength.

Scheme 2: use the "PING" command to check whether the network request is connected

Ping sends an ICMP(Internet Control Messages Protocol) to the destination and reports whether the desired ICMP echo (ICMP echo response) is received, which is used to check whether the network is smooth or the network connection speed.

By analyzing the relevant parameters in the result of each PING command, we can judge whether the network request is connected and how fast the access is. For example, packet loss rate, time consumption per packet and average time consumption.

4, Technical realization

// Declaring authority
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
// Scheme 1: judge the signal strength according to the connected wifi information
//(1) Get an instance of WiFi Manager
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService (Context.WIFI_SERVICE);
//(2) Get an instance of WiFi info
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//(3) Get related parameters
String wifiName = wifiInfo.getSSID();  //wifi name
String wifiBSSID = wifiInfo.getBSSID();  //Network access point
int wifiRssi = wifiInfo.getRssi();  //rssi value: signal strength
String wifiIP = Formatter.formatIpAddress(wifiInfo.getIpAddress());  //Access routing IP
int wifiLinkSpeed = wifiInfo.getLinkSpeed();  //Link speed
int wifiFrequency = wifiInfo.getFrequency();  //Current frequency
//Scheme 2: use the "PING" command to check whether the network request is connected
String ip = "www.baidu.com";// The address of ping. Baidu is currently ping
Process p = Runtime.getRuntime().exec("ping -c 4 -w 5 " + ip);// ping website 4 times
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String content = "";
while ((content = in.readLine()) != null) {stringBuffer.append(content);}
status = p.waitFor();  //Result of ping, 0-success, 1-failure
//stringBuffer.toString(): the result returned by ping, including packet loss rate, time consumption of each packet, average time consumption, etc

Five, summary

An APP is installed on Android device to realize WiFi status monitoring. Two schemes need to be combined:
1. Obtain the status information of the signal strength of the connected wifi router, and measure the signal strength of the intranet connected to the router
2. Judge whether it can access the external network and the speed of accessing the external network through the result of ping the external network address

Published 2 original articles, won praise 1, visited 3295
Private letter follow

Posted by Isomerizer on Thu, 05 Mar 2020 03:44:14 -0800