标签:反射 打开 pass src pretty 网络 cti 简单的 public
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
/**
* 创建Wifi热点
*/
private void createWifiHotspot() {
if (wifiManager.isWifiEnabled()) {
//如果wifi处于打开状态,则关闭wifi,
wifiManager.setWifiEnabled(false);
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = WIFI_HOTSPOT_SSID;
config.preSharedKey = "123456789";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
//通过反射调用设置热点
try {
Method method = wifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
boolean enable = (Boolean) method.invoke(wifiManager, config, true);
if (enable) {
textview.setText("热点已开启 SSID:" + WIFI_HOTSPOT_SSID + " password:123456789");
} else {
textview.setText("创建热点失败");
}
} catch (Exception e) {
e.printStackTrace();
textview.setText("创建热点失败");
}
}
This class provides the primary API for managing all aspects of Wi-Fi
connectivity. Get an instance of this class by calling
{@link android.content.Context#getSystemService(String) Context.getSystemService(Context.WIFI_SERVICE)}.
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
/**
* 关闭WiFi热点
*/
public void closeWifiHotspot() {
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();
}
}
Android WiFi开发教程(一)——WiFi热点的创建与关闭
标签:反射 打开 pass src pretty 网络 cti 简单的 public
原文地址:http://www.cnblogs.com/wanghuaijun/p/7148290.html