Android7.1.1版本实现wifi的打开关闭以及热点的设置


wifi打开关闭很简单,网上百度一大堆,问题主要集中在主要在于7.1.1热点的打开以及参数设置的问题。以前的各种方式已经不适用,主要代码调整 如下:


public boolean createAp(boolean isOpen) {
    //如果wifi已经打开则先关闭wifi
    closeWifi();
  //  Method method1=null;
    try {

        WifiConfiguration netConfig=new WifiConfiguration();

        netConfig.SSID=wifiBean.getApname();
        netConfig.preSharedKey=wifiBean.getAppasswd();
        Log.d(TAG, "WifiPresenter:createAp----->netConfig.SSID:"
                +netConfig.SSID+",netConfig.preSharedKey:"+netConfig.preSharedKey+",isOpen="+isOpen);
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        if (isOpen) {
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }else {
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        }
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            //保存ap设置
            Method methodsave = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
            methodsave.invoke(mWifiManager, netConfig);
            //获取ConnectivityManager对象,便于后面使用
            ConnectivityManager connManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

            Field field = connManager.getClass().getDeclaredField("TETHERING_WIFI");
            field.setAccessible(true);
            int mTETHERING_WIFI = (int)field.get(connManager);

            Field iConnMgrField = connManager.getClass().getDeclaredField("mService");
            iConnMgrField.setAccessible(true);
            Object iConnMgr = iConnMgrField.get(connManager);
            Class iConnMgrClass = Class.forName(iConnMgr.getClass().getName());
            Method startTethering = iConnMgrClass.getMethod("startTethering", int.class, ResultReceiver.class, boolean.class);
            startTethering.invoke(iConnMgr, mTETHERING_WIFI, new ResultReceiver(handler) {
                @Override
                protected void onReceiveResult(int resultCode, Bundle resultData) {
                    super.onReceiveResult(resultCode, resultData);
                }
            }, true);
            return true;

        } else {
            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            return (boolean) method.invoke(mWifiManager, netConfig, true);
        }

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return false;
}

你可能感兴趣的:(Android7.1.1版本实现wifi的打开关闭以及热点的设置)