[英]Android 2.2 wifi hotspot API
我需要在Android 2.2(Froyo)中创建一个API调用来创建一个Wifi热点(如Tethering和Portable Hotspot设置项中所示)。
You can call 你可以打电话
private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);
using reflection :) 使用反射:)
after getting the WifiManager
use the reflection to get the WifiManager
declared methods, look for this method name setWifiApEnabled
and invoke it through the WifiManager
object 获取WifiManager
使用反射获取WifiManager
声明的方法,查找此方法名称setWifiApEnabled
并通过WifiManager
对象调用它
These API are marked as @hide, so currently you cannot use them directly, but they appear on the AIDL for the WifiManager so their are accessible! 这些API标记为@hide,因此目前您无法直接使用它们,但它们出现在WifiManager的AIDL上,因此可以访问它们!
An example can be: 一个例子可以是:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "\"PROVAAP\"";
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);
try {
method.invoke(wifi, netConfig,true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
It works but I cannot change the current configuration with my own, and getting the current WifiConfiguration of an active AP drive me to an empty configuration.Why? 它工作但我不能用我自己的配置更改当前配置,并获得当前的活动AP的WifiConfiguration驱动我为空配置。为什么?
this works on API 8 and above. 这适用于API 8及更高版本。 I use a heavily different version then this below (or above), and was running into the same issue markov00 ran into; 我使用了一个完全不同的版本然后在下面(或上面),并遇到了同样的问题markov00遇到; not being able to load the default WifiConfiguration for the portable Wi-Fi AP. 无法加载便携式Wi-Fi AP的默认WifiConfiguration。 I found a solution elsewhere. 我在别处找到了解决方案。
If you like the solution, it would be nice if this was accepted as an answer 如果您喜欢这个解决方案,那么如果将其作为答案接受将会很好
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods){
if (method.getName().equals("setWifiApEnabled")){
try {
// just nullify WifiConfiguration to load the default configuration ;)
method.invoke(wifi, null, true);
} catch (IllegalArgumentException e){
e.printStackTrace();
} catch (IllegalAccessException e){
e.printStackTrace();
} catch (InvocationTargetException e){
e.printStackTrace();
}
}
}
似乎没有用于创建WiFi热点的API调用 - 抱歉!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.