Android手机热点连接信息查看

在测试一个APP时,需要手机作为热点等待设备连接,待设备连接后用APP进行操作。

为了获取手机热点WIFI连接情况,查阅了网上资料找到了下列命令用于手机WIFI状态查看。

1、手机是热点还是WIFI

WIFI有AP和STA两种模式,AP为热点,STA作为终端去连接热点。

用iw dev命令,手机如果开启了热点,则wlan0的type为AP,ssid为的SSID

如果手机连接了某个WIFI,则wlan0的type为managed,ssid为连接热点的SSID

iw还可以查看、设置网络物理层、应用层很多信息,不过该命令需要

# 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、查看连接的WIFI情况

通过cat /proc/net/arp命令可以查看WIFI连接。

如果手机为WIFI热点,则查询出来为连接热点的终端情况;

如果为手机连接WIFI热点,则查询出来为手机的网关地址;

注意Flags字段为0x2的时候表示连接正常,连接断开后Flags变为0x0(需要几秒钟),如果执行ip neigh flush dev wlan0则可立即刷新ARP状态信息(马上检测到连接或断开)。

如果WIFI连接中间断开了,则ARP信息不会清除,可以通过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

通过ip neigh show可以看到连接WIFI热点的实时状态,没有连接时adb shell ip neigh show查询为空

连接过程中,开始状态为短暂的INCOMPLETE状态,后为DELAY状态,稳定后状态为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

WIFI断开后状态变化顺序为:DELAY->PROBE->FAILED/INCOMPLETE

还有时候处于STALE状态

3、如何知道WIFI连接是否真的断开了

长期测试发现在WIFI没有流量的时候(比如黑屏状态),可能是为了省电或者其它原因,查询出来的对端状态不是完全准确。

比如用adb shell cat /proc/net/arp命令查询,对端一直连接热点,查询出来的状态大部分为0x2,少部分为0x0,也就是连接断开了,完全根据该命令判断不准确。

用adb shell ip neigh show命令查看也是类似的,对端状态在REACHABLE/DELAY/INCOMPLETE/STALE之间变化,不能准确判断是不是对端短暂的断开了连接。

后来想到一个方法,先在查询之前向对方ping几次,如果能ping通则说明是通的,否则不通

adb shell ping -c 3 IPV4

4、WLAN0抓包

可以通过tcpdump抓取手机的网络连接

tcpdump -i wlan0 -w /mnt/sdcard/Download/20200404.pcap

6、查看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功率

# iw dev wlan0 get power_save
Power save: on

phy set txpower []
Specify transmit power level and setting type.

dev set txpower []
Specify transmit power level and setting type.

8、WLAN0 IP地址获取

通过命令ifconfig wlan0获取IP地址,inet addr为IPV4地址。

WLAN0为热点

# 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关闭了热点和WIFI连接

# 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关闭了热点,打开WIFI但是没有连接任何热点:

# 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连上了WIFI热点

# 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、附录:ip neigh查询对端状态的含义

http://manpages.ubuntu.com/manpages/eoan/man8/ip-neighbour.8.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
   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.