Can't set “WifiConfiguration” when enabling wifi-hotspot using “setWifiApEnabled”

只愿长相守 提交于 2019-12-20 02:59:08

问题


I'm trying to set my Android device to be an Access-Point using the code I've seen here before:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyAccessPoint";

Method method = wifi.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifi, netConfig, true);

now, I managed to turning it on but without the SSID which I set in WifiConfiguration.

This is driving me crazy.

Anyone?


回答1:


See how I got this working at Android 2.3 wifi hotspot API.




回答2:


Before Invoking the Method "setWifiApEnabled" you need to call "getWifiApConfiguration" to get the default WifiConfiguration
Then change the SSID and Password and then invoke "setWifiApConfiguration" with the modified WifiConfiguration and after that call "setWifiApEnabled"
Here is the Code.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

getWifiConfig = wifi.getClass().getMethod("getWifiApConfiguration",null);
WifiConfiguration myConfig = (WifiConfiguration) getWifiConfig.invoke(wifi,null);

myConfig.SSID = "Hello World";

setWifiConfig = wifi.getClass().getMethod("setWifiApConfiguration",WifiConfiguration.class);
setWifiConfig.invoke(wifi,new Object[]{myConfig,true});

enableWifi = wifi.getClass().getMethod("setWifiEnabled",WifiConfiguration.class,boolean.class);
enableWifi.invoke(wifi,null,true);


来源:https://stackoverflow.com/questions/7221712/cant-set-wificonfiguration-when-enabling-wifi-hotspot-using-setwifiapenabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!