我正在使用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面板中看到“已连接”)到在笔记本电脑上创建的临时网络。没有互联网访问。
ssCOMPLETEDdsOBTAINING_IPADDRip-745953088(192.168.137.211)

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

最佳答案

查看类 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来检查状态是否已连接。

10-08 13:59