When testing an APP, the mobile phone needs to be used as a hotspot to wait for the device to connect. After the device is connected, use the APP to operate.
In order to obtain the WIFI connection status of the mobile phone hotspot, I checked the online information and found the following commands for the mobile phone WIFI status check.
1. Is the phone a hotspot or WIFI
WIFI has two modes: AP and STA. AP is a hotspot and STA is used as a terminal to connect to the hotspot.
With the iw dev command, if the hotspot is turned on on the phone, the type of wlan0 is AP and the SSID of ssid is
If the phone is connected to a WIFI, the type of wlan0 is managed, and ssid is the SSID of the hotspot connected
iw can also view and set a lot of information on the network physical layer and application layer, but this command requires
# iw dev
phy#0
Interface p2p0
ifindex 27
wdev 0x2
addr XXXXXXXXXXXX
type P2P-device
Interface wlan0
ifindex 26
wdev 0x1
addr XXXXXXXXXXXX
ssid TEST
type AP
2. Check the connected WIFI situation
You can view the WIFI connection through the cat /proc/net/arp command.
If the mobile phone is a WIFI hotspot, the information of the terminal connected to the hotspot will be queried;
If the mobile phone is connected to the WIFI hotspot, the gateway address of the mobile phone will be queried;
Note that when the Flags field is 0x2, it means that the connection is normal. After the connection is disconnected, the Flags becomes 0x0 (it takes a few seconds). If you execute ip neigh flush dev wlan0, the ARP status information can be refreshed immediately (the connection or disconnection is detected immediately).
If the WIFI connection is disconnected in the middle, the ARP information will not be cleared. You can check the current connection status through Flags.
C:\Windows\System32>adb shell cat /proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.43.181 0x1 0x2 XXXXXXXXXXXX * wlan0
C:\Windows\System32>adb shell cat /proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.43.1 0x1 0x2 XXXXXXXXXXXX * wlan0
Through ip neigh show, you can see the real-time status of connecting to WIFI hotspot. When there is no connection, the query of adb shell ip neigh show is empty
During the connection process, the initial state is a short INCOMPLETE state, then the DELAY state, and the stable state is REACHABLE.
C:\Windows\System32>adb shell ip neigh show
192.168.43.181 dev wlan0 lladdr 20:f4:78:09:48:b3 DELAY
fe80::22f4:78ff:fe09:48b3 dev wlan0 lladdr 20:f4:78:09:48:b3 DELAY
2409:894c:130:10bc:d:1b8e:e8ea:3b2c dev wlan0 lladdr 20:f4:78:09:48:b3 REACHABLE
C:\Windows\System32>adb shell ip neigh show
192.168.43.181 dev wlan0 lladdr 20:f4:78:09:48:b3 REACHABLE
fe80::22f4:78ff:fe09:48b3 dev wlan0 lladdr 20:f4:78:09:48:b3 REACHABLE
2409:894c:130:10bc:d:1b8e:e8ea:3b2c dev wlan0 lladdr 20:f4:78:09:48:b3 DELAY
The sequence of state changes after WIFI disconnection is: DELAY->PROBE->FAILED/INCOMPLETE
Sometimes in STALE state
3. How to know if the WIFI connection is really disconnected
Long-term testing found that when there is no traffic on the WIFI (such as the black screen state), it may be for power saving or other reasons, and the peer state queried is not completely accurate.
For example, if you use the adb shell cat /proc/net/arp command to query, the peer has been connected to the hotspot. Most of the queried status is 0x2, and a small part is 0x0, that is, the connection is disconnected. The judgment based on this command is completely inaccurate.
Using the adb shell ip neigh show command to view is similar. The status of the opposite end changes between REACHABLE/DELAY/INCOMPLETE/STALE, and it is impossible to accurately determine whether the opposite end has temporarily disconnected.
Later, I thought of a way, first ping the other party several times before querying, if it can ping, it means it works, otherwise it doesn’t work
adb shell ping -c 3 IPV4
4. WLAN0 packet capture
You can grab the phone's network connection through tcpdump
tcpdump -i wlan0 -w /mnt/sdcard/Download/20200404.pcap
6. Check the receiving and sending packets of WLAN0
# iw dev wlan0 station dump
Station XXXXXXXXXXXX (on wlan0)
rx packets: 3558
tx packets: 3355
tx retries: 170
tx failed: 6
# iw dev wlan0 station get XXXXXXXXXXXX
Station 20:f4:78:09:48:b3 (on wlan0)
rx packets: 1035
tx packets: 982
tx retries: 29
tx failed: 0
7. WLAN power
# iw dev wlan0 get power_save
Power save: on
phy <phyname> set txpower <auto|fixed|limit> [<tx power in mBm>]
Specify transmit power level and setting type.
dev <devname> set txpower <auto|fixed|limit> [<tx power in mBm>]
Specify transmit power level and setting type.
8. WLAN0 IP address acquisition
Obtain the IP address through the command ifconfig wlan0, and inet addr is the IPV4 address.
WLAN0 is a hotspot
# ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr XXXXXXXXXXXX Driver icnss
inet addr:192.168.43.1 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::b6c4:fcff:fe5c:3d60/64 Scope: Link
inet6 addr: 2409:894c:130:10bc::88/64 Scope: Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6419 errors:0 dropped:70 overruns:0 carrier:0
collisions:0 txqueuelen:3000
RX bytes:0 TX bytes:2586348
WLAN0 turned off the hotspot and WIFI connection
# ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr XXXXXXXXXXXX Driver icnss
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6430 errors:0 dropped:70 overruns:0 carrier:0
collisions:0 txqueuelen:3000
RX bytes:0 TX bytes:2587230
WLAN0 turns off the hotspot, turns on WIFI but no hotspot is connected:
# ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr XXXXXXXXXXXX Driver icnss
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:446 errors:0 dropped:0 overruns:0 frame:0
TX packets:7061 errors:0 dropped:70 overruns:0 carrier:0
collisions:0 txqueuelen:3000
RX bytes:93909 TX bytes:2794442
WLAN0 is connected to WIFI hotspot
# ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr XXXXXXXXXXXX Driver icnss
inet addr:192.168.43.18 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::b6c4:fcff:fe5c:3d60/64 Scope: Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:54 errors:0 dropped:0 overruns:0 frame:0
TX packets:6501 errors:0 dropped:70 overruns:0 carrier:0
collisions:0 txqueuelen:3000
RX bytes:8359 TX bytes:2598189
9. Appendix: the meaning of ip neigh querying the status of the peer
http://manpages.ubuntu.com/manpages/eoan/man8/ip-neighbour.8.html
The ip neigh command manipulates neighbour objects that establish bindings between
protocol addresses and link layer addresses for hosts sharing the same link. Neighbour
entries are organized into tables. The IPv4 neighbour table is also known by another name
- the ARP table.
The corresponding commands display neighbour bindings and their properties, add new
neighbour entries and delete old ones.
ip neighbour add
add a new neighbour entry
ip neighbour change
change an existing entry
ip neighbour replace
add a new entry or change an existing one
These commands create new neighbour records or update existing ones.
to ADDRESS (default)
the protocol address of the neighbour. It is either an IPv4 or IPv6 address.
dev NAME
the interface to which this neighbour is attached.
proxy indicates whether we are proxying for this neigbour entry
router indicates whether neigbour is a router
extern_learn
this neigh entry was learned externally. This option can be used to indicate
to the kernel that this is a controller learnt dynamic entry. Kernel will
not gc such an entry.
lladdr LLADDRESS
the link layer address of the neighbour. LLADDRESS can also be null.
nud STATE
the state of the neighbour entry. nud is an abbreviation for 'Neighbour
Unreachability Detection'. The state can take one of the following values:
permanent
the neighbour entry is valid forever and can be only be removed
administratively.
noarp the neighbour entry is valid. No attempts to validate this entry will
be made but it can be removed when its lifetime expires.
reachable
the neighbour entry is valid until the reachability timeout expires.
stale the neighbour entry is valid but suspicious. This option to ip neigh
does not change the neighbour state if it was valid and the address
is not changed by this command.
none this is a pseudo state used when initially creating a neighbour entry
or after trying to remove it before it becomes free to do so.
incomplete
the neighbour entry has not (yet) been validated/resolved.
delay neighbor entry validation is currently delayed.
probe neighbor is being probed.
failed max number of probes exceeded without success, neighbor validation
has ultimately failed.
ip neighbour delete
delete a neighbour entry
The arguments are the same as with ip neigh add, except that lladdr and nud are
ignored.
Warning: Attempts to delete or manually change a noarp entry created by the kernel
may result in unpredictable behaviour. Particularly, the kernel may try to resolve
this address even on a NOARP interface or if the address is multicast or broadcast.
ip neighbour show
list neighbour entries
to ADDRESS (default)
the prefix selecting the neighbours to list.
dev NAME
only list the neighbours attached to this device.
vrf NAME
only list the neighbours for given VRF.
proxy list neighbour proxies.
unused only list neighbours which are not currently in use.
nud STATE
only list neighbour entries in this state. NUD_STATE takes values listed
below or the special value all which means all states. This option may occur
more than once. If this option is absent, ip lists all entries except for
none and noarp.
ip neighbour flush
flush neighbour entries
This command has the same arguments as show. The differences are that it does not
run when no arguments are given, and that the default neighbour states to be
flushed do not include permanent and noarp.
With the -statistics option, the command becomes verbose. It prints out the number
of deleted neighbours and the number of rounds made to flush the neighbour table.
If the option is given twice, ip neigh flush also dumps all the deleted neighbours.