Force Android to Use Wifi network with no internet

前端 未结 5 912
猫巷女王i
猫巷女王i 2020-12-24 06:23

I am building an android app that needs to communicate over a WiFi network that will not have any internet access. The problem is that even when the WiFi is connected androi

5条回答
  •  心在旅途
    2020-12-24 06:47

    You can check if wifi is connected then proceed else show a dialog to user asking him to connect to a wifi network

    Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:

    private boolean checkWifiOnAndConnected() {
        WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    
        if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON
    
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    
            if( wifiInfo.getNetworkId() == -1 ){
                return false; // Not connected to an access point
            }
            return true; // Connected to an access point
        }
        else {
            return false; // Wi-Fi adapter is OFF
        }
    }
    

提交回复
热议问题