因为无线wifi不能和wufi热点同时打开,所以开启wifi热点的时候要监测是否打开了wifi,打开了就要关闭
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
private void startWifiAp() { handler = new Handler(); if (wifiManager.isWifiEnabled()) { closeWifiHandler = new Handler() { @Override public void handleMessage(Message msg) { startWifiApTh(); super.handleMessage(msg); } }; cwt = new CloseWifiThread(); Thread thread = new Thread(cwt); thread.start(); }else{ startWifiApTh(); } } class CloseWifiThread implements Runnable { public CloseWifiThread() { super(); } @Override public void run() { int state = wifiManager.getWifiState(); if (state == WifiManager.WIFI_STATE_ENABLED) { wifiManager.setWifiEnabled(false); closeWifiHandler.postDelayed(cwt, 1000); } else if (state == WifiManager.WIFI_STATE_DISABLING) { closeWifiHandler.postDelayed(cwt, 1000); }else if(state == WifiManager.WIFI_STATE_DISABLED){ closeWifiHandler.sendEmptyMessage(0); } } } private void startWifiApTh(){ swat = new StratWifiApThread(); Thread thread = new Thread(swat); thread.start(); } class StratWifiApThread implements Runnable { public StratWifiApThread() { super(); } @Override public void run() { // TODO Auto-generated method stub WifiUtil mWifiUtil = new WifiUtil(); int state = mWifiUtil.getWifiApState(wifiManager); if (state == WifiUtil.WIFI_AP_STATE_DISABLED) { mWifiUtil.stratWifiAp(wifiManager); handler.postDelayed(swat, 1000); } else if (state == WifiUtil.WIFI_AP_STATE_ENABLING || state == WifiUtil.WIFI_AP_STATE_FAILED) { handler.postDelayed(swat, 1000); }else if (state == WifiUtil.WIFI_AP_STATE_ENABLED){ Toast.makeText(ActivityForAndroid.this, "已开启wlan热点", 2000).show(); } } }
public class WifiUtil { public static final int WIFI_AP_STATE_DISABLING = 0; public static final int WIFI_AP_STATE_DISABLED = 1; public static final int WIFI_AP_STATE_ENABLING = 2; public static final int WIFI_AP_STATE_ENABLED = 3; public static final int WIFI_AP_STATE_FAILED = 4; public void stratWifiAp(WifiManager wifiManager) { // WifiManager wifi = (WifiManager) // getSystemService(Context.WIFI_SERVICE); // wifiManager.setWifiEnabled(false); Method method1 = null; try { method1 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); WifiConfiguration netConfig = new WifiConfiguration(); //wifi热点名字 netConfig.SSID = "\" T E S T \""; 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); //密码 netConfig.preSharedKey = "\"11111111\""; method1.invoke(wifiManager, netConfig, true); // Method method2 = // wifiManager.getClass().getMethod("getWifiApState"); // int state = (Integer) method2.invoke(wifiManager); // Log.i("wifi state" + state); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int getWifiApState(WifiManager wifiManager) { try { Method method = wifiManager.getClass().getMethod("getWifiApState"); int i = (Integer) method.invoke(wifiManager); Log.i("wifi state: " + i); return i; } catch (Exception e) { Log.i("Cannot get WiFi AP state" + e); return WIFI_AP_STATE_FAILED; } } }
wifiutil类的打开wifi热点的代码用到java的反射类 还在学习..