实际连接到Wifi时SupplicantState COMPLETED但DetailedState OBTAINING_IPADDR?

时间:2017-12-19 17:23:47

标签: android android-6.0-marshmallow android-wifi wifimanager

我正在使用API​​级别27,但在6.0平板电脑上运行我的应用程序。这是可疑的代码:

WifiManager wm = (WifiManager)pContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
SupplicantState ss = wi.getSupplicantState();
NetworkInfo.DetailedState ds = wi.getDetailedStateOf( ss );
String ssid = wi.getSSID();
int ip = wi.getIpAddress();

我的平板电脑已连接(我在WiFi面板中看到“已连接”)到笔记本电脑上创建的ad hoc网络。没有互联网接入。

ssCOMPLETED

dsOBTAINING_IPADDR

ip-745953088(192.168.137.211)

为什么DetailedState设置为OBTAINING_IPADDR而不是CONNECTED

1 个答案:

答案 0 :(得分:0)

查看类 android.net.wifi.WifiInfo

static {
    stateMap.put(SupplicantState.DISCONNECTED, DetailedState.DISCONNECTED);
    stateMap.put(SupplicantState.INTERFACE_DISABLED, DetailedState.DISCONNECTED);
    stateMap.put(SupplicantState.INACTIVE, DetailedState.IDLE);
    stateMap.put(SupplicantState.SCANNING, DetailedState.SCANNING);
    stateMap.put(SupplicantState.AUTHENTICATING, DetailedState.CONNECTING);
    stateMap.put(SupplicantState.ASSOCIATING, DetailedState.CONNECTING);
    stateMap.put(SupplicantState.ASSOCIATED, DetailedState.CONNECTING);
    stateMap.put(SupplicantState.FOUR_WAY_HANDSHAKE, DetailedState.AUTHENTICATING);
    stateMap.put(SupplicantState.GROUP_HANDSHAKE, DetailedState.AUTHENTICATING);
    stateMap.put(SupplicantState.COMPLETED, DetailedState.OBTAINING_IPADDR);
    stateMap.put(SupplicantState.DORMANT, DetailedState.DISCONNECTED);
    stateMap.put(SupplicantState.UNINITIALIZED, DetailedState.IDLE);
    stateMap.put(SupplicantState.INVALID, DetailedState.FAILED);
}

您会看到 DetailedState.CONNECTED 未映射任何SupplicantState值。不知道为什么。

使用 SupplicantState 而不是DetailedState来检查状态是否已连接。