Android examples for android.net.wifi:WifiManager
create WiFi Hot Spot
import java.lang.reflect.Method; import java.util.List; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.util.Log; public class Main { public static final int TYPE_WPA = 3; public static boolean createAP(WifiManager wifiManager, String ssid, String pwd, boolean delete) { WifiConfiguration exists = getExistingWifi(wifiManager, ssid); if (exists != null) { if (exists.status == WifiConfiguration.Status.CURRENT && ssid.equals(exists.SSID) && pwd.equals(exists.preSharedKey)) { return true; } else {/*from w ww .ja v a 2 s . co m*/ removeWifi(wifiManager, exists); } } wifiManager.setWifiEnabled(false); if (getWifiApState(wifiManager)) { WifiConfiguration currentAp = getWifiApConfiguration(wifiManager); if (currentAp != null) { if (currentAp.status == WifiConfiguration.Status.CURRENT && ssid.equals(currentAp.SSID) && pwd.equals(currentAp.preSharedKey)) { return true; } else { setWifiApEnabled(wifiManager, currentAp, false); } } } WifiConfiguration wifi = createWifiConfiguration(wifiManager, ssid, pwd, TYPE_WPA); return setWifiApEnabled(wifiManager, wifi, true); } public static boolean getWifiApState(WifiManager wifiManager) { try { return ((Boolean) wifiManager.getClass().getMethod("isWifiApEnabled", new Class[0]).invoke(wifiManager, new Object[0])); } catch (Exception e) { e.printStackTrace(); } return false; } public static WifiConfiguration createWifiConfiguration(WifiManager wifiManager, String ssid, String password, int type) { Log.d("", "test createWifiInfo ssid" + ssid + " password:" + password + " type:" + type); WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.allowedAuthAlgorithms.clear(); wifiConfiguration.allowedGroupCiphers.clear(); wifiConfiguration.allowedKeyManagement.clear(); wifiConfiguration.allowedPairwiseCiphers.clear(); wifiConfiguration.allowedProtocols.clear(); { wifiConfiguration.SSID = ssid; wifiConfiguration.allowedAuthAlgorithms.set(1); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); wifiConfiguration.allowedKeyManagement.set(0); wifiConfiguration.wepTxKeyIndex = 0; if (type == 1) { wifiConfiguration.wepKeys[0] = ""; wifiConfiguration.allowedKeyManagement.set(0); wifiConfiguration.wepTxKeyIndex = 0; } else if (type == 2) { wifiConfiguration.hiddenSSID = true; wifiConfiguration.wepKeys[0] = password; } else if (type == 3) { wifiConfiguration.preSharedKey = password; wifiConfiguration.allowedAuthAlgorithms.set(0); wifiConfiguration.allowedProtocols.set(1); wifiConfiguration.allowedProtocols.set(0); wifiConfiguration.allowedKeyManagement.set(1); wifiConfiguration.allowedPairwiseCiphers.set(2); wifiConfiguration.allowedPairwiseCiphers.set(1); } } return wifiConfiguration; } public static WifiConfiguration getWifiApConfiguration(WifiManager wifiManager) { try { return ((WifiConfiguration) wifiManager.getClass().getMethod("getWifiApConfiguration", new Class[0]) .invoke(wifiManager, new Object[0])); } catch (Exception e) { e.printStackTrace(); } return null; } public static WifiConfiguration getExistingWifi(WifiManager wifiManager, String ssid) { List<WifiConfiguration> netWorkList = wifiManager.getConfiguredNetworks(); if (netWorkList == null) { return null; } String realSSID = "\"" + ssid + "\""; for (WifiConfiguration wifi : netWorkList) { if (wifi != null && realSSID.equals(wifi.SSID)) { return wifi; } } return null; } public static boolean setWifiApEnabled(WifiManager wifiManager, WifiConfiguration paramWifiConfiguration, boolean enable) { try { Class<? extends WifiManager> localClass = wifiManager.getClass(); Class<?>[] arrayOfClass = new Class[2]; arrayOfClass[0] = WifiConfiguration.class; arrayOfClass[1] = Boolean.TYPE; Method localMethod = localClass.getMethod("setWifiApEnabled", arrayOfClass); WifiManager localWifiManager = wifiManager; Object[] arrayOfObject = new Object[2]; arrayOfObject[0] = paramWifiConfiguration; arrayOfObject[1] = Boolean.valueOf(enable); return (Boolean) localMethod.invoke(localWifiManager, arrayOfObject); } catch (Exception e) { e.printStackTrace(); } return false; } public static boolean removeWifi(WifiManager wifiManager, WifiConfiguration wifi) { return wifiManager.removeNetwork(wifi.networkId); } }