How do I see if Wi-Fi is connected on Android?

前端 未结 22 2586
挽巷
挽巷 2020-11-22 05:56

I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti

22条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:15

    I simply use the following:

    SupplicantState supState; 
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    supState = wifiInfo.getSupplicantState();
    

    Which will return one of these states at the time you call getSupplicantState();

    ASSOCIATED - Association completed.

    ASSOCIATING - Trying to associate with an access point.

    COMPLETED - All authentication completed.

    DISCONNECTED - This state indicates that client is not associated, but is likely to start looking for an access point.

    DORMANT - An Android-added state that is reported when a client issues an explicit DISCONNECT command.

    FOUR_WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress.

    GROUP_HANDSHAKE - WPA Group Key Handshake in progress.

    INACTIVE - Inactive state.

    INVALID - A pseudo-state that should normally never be seen.

    SCANNING - Scanning for a network.

    UNINITIALIZED - No connection.

提交回复
热议问题