Android WiFi connection programmatically

前端 未结 4 576
没有蜡笔的小新
没有蜡笔的小新 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:22

    The code posted from AnujAroshA here is really helpful. But there is an else case missing:

      ..
      } else if (securityMode.equalsIgnoreCase("PSK")) {
        // PSK code
      ..
    

    Otherwise the PSK code is executed for WEP and OPEN networks, too.

    And code style is not optimal in my opinion. It is unnecessary to create the two Strings networkSSID and networkPass. You can simply use ssid or passkey.

    Suggestion: Split creating the wifi configuration and connect method.

    (1) Method for creating the WifiConfiguration:

    private WifiConfiguration createAPConfiguration(String networkSSID, String networkPasskey, String securityMode) {
            WifiConfiguration wifiConfiguration = new WifiConfiguration();
    
            wifiConfiguration.SSID = "\"" + networkSSID + "\"";
    
            if (securityMode.equalsIgnoreCase("OPEN")) {
    
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    
            } else if (securityMode.equalsIgnoreCase("WEP")) {
    
                wifiConfiguration.wepKeys[0] = "\"" + networkPasskey + "\"";
                wifiConfiguration.wepTxKeyIndex = 0;
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    
            } else if (securityMode.equalsIgnoreCase("PSK")) {
    
                wifiConfiguration.preSharedKey = "\"" + networkPasskey + "\"";
                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);
    
            } else {
                Log.i(TAG, "# Unsupported security mode: "+securityMode);
    
                return null;
            }
    
            return wifiConfiguration;
    
        }
    

    (2) Method for the AP connect:

        public int connectToAP(String networkSSID, String networkPasskey) {
        for (ScanResult result : scanResultList) {
    
            if (result.SSID.equals(networkSSID)) {
    
                String securityMode = getScanResultSecurity(result);
    
                WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode);
    
                int res = wifiManager.addNetwork(wifiConfiguration);
                Log.d(TAG, "# addNetwork returned " + res);
    
                boolean b = wifiManager.enableNetwork(res, true);
                Log.d(TAG, "# enableNetwork returned " + b);
    
                wifiManager.setWifiEnabled(true);
    
                boolean changeHappen = wifiManager.saveConfiguration();
    
                if (res != -1 && changeHappen) {
                    Log.d(TAG, "# Change happen");
                    connectedSsidName = networkSSID;
                } else {
                    Log.d(TAG, "# Change NOT happen");
                }
    
                return res;
            }
        }
    
        return -1;
    }
    

    (3) The scan method 100% as posted AnujAroshA.

    public String getScanResultSecurity(ScanResult scanResult) {
    
        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";
    }
    

提交回复
热议问题