Android WiFi connection programmatically

前端 未结 4 569
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:16

Do you have any idea how to establish a wifi connection with sending password in my android application?

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 19:36

    Pass the SSID and it's password to the following method.

    public void connectToAP(String ssid, String passkey) {
        Log.i(TAG, "* connectToAP");
    
        WifiConfiguration wifiConfiguration = new WifiConfiguration();
    
        String networkSSID = ssid;
        String networkPass = passkey;
    
        Log.d(TAG, "# password " + networkPass);
    
        for (ScanResult result : scanResultList) {
            if (result.SSID.equals(networkSSID)) {
    
                String securityMode = getScanResultSecurity(result);
    
                if (securityMode.equalsIgnoreCase("OPEN")) {
    
                    wifiConfiguration.SSID = "\"" + networkSSID + "\"";
                    wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                    int res = wifiManager.addNetwork(wifiConfiguration);
                    Log.d(TAG, "# add Network returned " + res);
    
                    boolean b = wifiManager.enableNetwork(res, true);
                    Log.d(TAG, "# enableNetwork returned " + b);
    
                    wifiManager.setWifiEnabled(true);
    
                } else if (securityMode.equalsIgnoreCase("WEP")) {
    
                    wifiConfiguration.SSID = "\"" + networkSSID + "\"";
                    wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
                    wifiConfiguration.wepTxKeyIndex = 0;
                    wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                    wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                    int res = wifiManager.addNetwork(wifiConfiguration);
                    Log.d(TAG, "### 1 ### add Network returned " + res);
    
                    boolean b = wifiManager.enableNetwork(res, true);
                    Log.d(TAG, "# enableNetwork returned " + b);
    
                    wifiManager.setWifiEnabled(true);
                }
    
                wifiConfiguration.SSID = "\"" + networkSSID + "\"";
                wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
                wifiConfiguration.hiddenSSID = true;
                wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
                wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    
                int res = wifiManager.addNetwork(wifiConfiguration);
                Log.d(TAG, "### 2 ### add Network returned " + res);
    
                wifiManager.enableNetwork(res, true);
    
                boolean changeHappen = wifiManager.saveConfiguration();
    
                if(res != -1 && changeHappen){
                    Log.d(TAG, "### Change happen");
    
                    AppStaticVar.connectedSsidName = networkSSID;
    
                }else{
                    Log.d(TAG, "*** Change NOT happen");
                }
    
                wifiManager.setWifiEnabled(true);
            }
        }
    }
    
    public String getScanResultSecurity(ScanResult scanResult) {
        Log.i(TAG, "* getScanResultSecurity");
    
        final String cap = scanResult.capabilities;
        final String[] securityModes = { "WEP", "PSK", "EAP" };
    
        for (int i = securityModes.length - 1; i >= 0; i--) {
            if (cap.contains(securityModes[i])) {
                return securityModes[i];
            }
        }
    
        return "OPEN";
    }
    

    Don't forget to add necessary permission in the Manifest file.

提交回复
热议问题