美文网首页
开启WiFi热点的工具类

开启WiFi热点的工具类

作者: 阳光下的狐狸 | 来源:发表于2017-01-04 17:02 被阅读0次

    开启WiFi热点时候WiFi是不能开启的

    package com.example.until;
    
    import java.lang.reflect.InvocationTargetException;
    
    import java.lang.reflect.Method;
    
    import android.content.Context;
    
    import android.net.wifi.WifiConfiguration;
    
    import android.net.wifi.WifiManager;
    
    import android.util.Log;
    
    public class WifiApAdmin {
    
        public static final String TAG = "WifiApAdmin";
        private WifiManager mWifiManager = null;
        private Context mContext = null;
    
        public WifiApAdmin(Context context) {
            mContext = context;
            mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            closeWifiAp(mWifiManager);
        }
      //  开启WiFi热点所需要的名字跟密码
    
        public void startWifiAp(String ssid, String passwd) {
            mSSID = ssid;
            mPasswd = passwd;
            if (mWifiManager.isWifiEnabled()) {
                mWifiManager.setWifiEnabled(false);
            }
            stratWifiAp();
            MyTimerCheck timerCheck = new MyTimerCheck() {
    
                @Override
                public void doTimerCheckWork() {
                    if (isWifiApEnabled(mWifiManager)) {
                        Log.v(TAG, "Wifi enabled success!");
                        this.exit();
                    } else {
                        Log.v(TAG, "Wifi enabled failed!");
                    }
                }
    
                @Override
                public void doTimeOutWork() {
                    this.exit();
                }
            };
            timerCheck.start(15, 1000);
        }
    
        public void stratWifiAp() {
            Method method1 = null;
            try {
                method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = mSSID;
                if (" ".equals(mPasswd)) {
                    netConfig.wepKeys[0] = "";
                    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                    netConfig.wepTxKeyIndex = 0;
                } else {
                    netConfig.preSharedKey = mPasswd;
                    netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                    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);
                }
                method1.invoke(mWifiManager, netConfig, true);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
      //  关启WiFi热点
    
    public static void closeWifiAp(Context context) {
    
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            closeWifiAp(wifiManager);
        }
    
        private static void closeWifiAp(WifiManager wifiManager) {
            if (isWifiApEnabled(wifiManager)) {
                try {
                    Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
                    method.setAccessible(true);
                    WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
                    Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,
                            boolean.class);
                    method2.invoke(wifiManager, config, false);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    //判断WiFi热点是否开启
    
        private static boolean isWifiApEnabled(WifiManager wifiManager) {
            try {
                Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
                method.setAccessible(true);
                return (Boolean) method.invoke(wifiManager);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }
    

    相关文章

      网友评论

          本文标题:开启WiFi热点的工具类

          本文链接:https://www.haomeiwen.com/subject/cazovttx.html