删除或断开android marshmallow中的特定wifi网络?

时间:2016-07-15 07:48:08

标签: android wifi android-wifi android-6.0-marshmallow wificonfiguration

 if (Build.VERSION.SDK_INT >= 23) {

            wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            wifi.startScan();

            /////////////////////
            beforeOpenDoorWifi = wifi.getConnectionInfo().getNetworkId();
            wifi.setWifiEnabled(true);
            // setup a wifi configuration
            WifiConfiguration wc = new WifiConfiguration();

            List lst = wifi.getConfiguredNetworks();


            wc.SSID = "\"" + sSid + "\"";
            wc.preSharedKey = "\"" + pass + "\"";

            wc.status = WifiConfiguration.Status.DISABLED;
            wc.priority = 40;
            wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

            // connect to and enable the connection

            int netId = wifi.addNetwork(wc);
            if (-1 == netId) {
                // enableWifi(wifiName);

                List<WifiConfiguration> llst = wifi.getConfiguredNetworks();
                Iterator<WifiConfiguration> it = llst.iterator();
                while (it.hasNext()) {
                    WifiConfiguration wconfig = it.next();
                    if (wconfig.SSID.equals("\"" + sSid + "\"")) {
                        wifi.enableNetwork(wconfig.networkId, true);

                        setFalseOther(mm);
                        notifyDataSetChanged();
                        mSession.setPrefrenceString("networkName", sSid);
                        Intent i = new Intent(mContext, Connect.class);
                        i.putExtra("networkName", mm.getName());
                        i.putExtra("networkId", mm.getId());
                        i.putExtra("AdminID", mm.getUserId());
                        i.putExtra("networkConnection", "true");

                        mContext.startActivity(i);

                        break;
                    }
                }

此代码适用于连接到给定的SSID和密码但我想断开或删除该网络详细信息。&#34; disableNetwork(networkId)&#34;不给任何帮助。如果任何人可以提供更好的解决方案连接,断开或删除棉花糖中的wifi网络将是有帮助的。提前谢谢。

1 个答案:

答案 0 :(得分:0)

        public void forget(final String s) {
    final WifiConfiguration wifiConfigure = this.getWifiConfigure(s);
    if (wifiConfigure != null) {
        final Method[] declaredMethods = this.mWifiManager.getClass().getDeclaredMethods();
        Boolean b = false;
        for (final Method method : declaredMethods) {
            if (method.getName().equals("forget")) {
                b = true;
                try {

                    method.invoke(this.mWifiManager, wifiConfigure.networkId, null);
                }
                catch (Exception ex) {
                    b = false;
                }
                break;
            }
        }
        if (!b) {
            this.disableAndRemoveWifi(s);
        }
    }
}
相关问题