Android Connect to a Wi-Fi

Notes

    /**
     * Log tag
     */
    private static final String TAG = "Permission";

    /**
     * Wi-Fi Manager
     */
    private WifiManager wifiManager;

    /**
     * Just for the test members
     */
    private void connectToAWifi() {
        requestAppPermissions();

        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        final String ssid = "Wifi Name";
        final String password = "Password";

        if (wifiManager != null && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        int netId = wifiManager.addNetwork(createWifiConfig(ssid, password, WPA));
        boolean enable = wifiManager.enableNetwork(netId, true);
        if (!enable) {
            wifiManager.reconnect();
        }
    }

    /**
     * Password type
     */
    private static final int OPEN = 0;
    private static final int WEP = 1;
    private static final int WPA = 2;

    /**
     * Create Wi-Fi config
     *
     * @param ssid Wi-Fi SSID
     * @param password Wi-Fi password
     * @param type Password type
     * @return Created config
     */
    private WifiConfiguration createWifiConfig(String ssid, String password, int type) {

        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();

        config.SSID = "\"" + ssid + "\"";

        WifiConfiguration tempConfig = isExist(ssid);
        if (tempConfig != null) {
            wifiManager.removeNetwork(tempConfig.networkId);
        }

        if (type == OPEN) {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        } else if (type == WEP) {
            config.hiddenSSID = true;
            config.wepKeys[0] = "\"" + password + "\"";
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            config.wepTxKeyIndex = 0;
        } else if (type == WPA) {
            config.preSharedKey = "\"" + password + "\"";
            config.hiddenSSID = true;
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            config.status = WifiConfiguration.Status.ENABLED;
        }

        return config;
    }

    private WifiConfiguration isExist(String ssid) {
        List configs = wifiManager.getConfiguredNetworks();

        for (WifiConfiguration config : configs) {
            if (config.SSID.equals("\"" + ssid + "\"")) {
                return config;
            }
        }
        return null;
    }

    /**
     * Request this app's all denied permissions
     */
    public void requestAppPermissions() {
        String TAG = "Permission";
        PackageManager pm = getPackageManager();
        List deniedPermissions = new ArrayList<>();
        try {
            PackageInfo info = pm.getPackageInfo(getPackageName(), PackageManager.GET_PERMISSIONS);
            String[] permissions = info.requestedPermissions;
            if (permissions == null) {
                return;
            }
            for (String p : permissions) {
                Log.e(TAG, "p = " + p);
                if (pm.checkPermission(p, getPackageName()) != PackageManager.PERMISSION_GRANTED) {
                    deniedPermissions.add(p);
                    Log.e(TAG, "denied p = " + p);
                }
            }

            String[] pArray = new String[deniedPermissions.size()];
            pArray = deniedPermissions.toArray(pArray);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && pArray.length > 0) {
                requestPermissions(pArray, 1024);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Android Connect to a Wi-Fi)