3

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?

animuson
  • 53,861
  • 28
  • 137
  • 147
xhuur
  • 103
  • 1
  • 7
  • I'm no expert on this but have you checked your manifest for the correct permissions ? – D-Dᴙum Aug 28 '11 at 15:10
  • @Kerubu Yes, you are right, this might be the issue. @FLEXpert Try adding `CHANGE_WIFI_STATE` `CHANGE_WIFI_MULTICAST_STATE` and `ACCESS_WIFI_STATE` permissions in your manifest. http://developer.android.com/reference/android/Manifest.permission.html#CHANGE_WIFI_STATE – Nikola Despotoski Aug 28 '11 at 17:27
  • I seem to remember having a few issues of this myself and vaguely remember reading that android fails silently if an app does not have the correct permissions hence programmers are left in the dark a bit! – D-Dᴙum Aug 28 '11 at 18:33
  • 1
    ok, I finally managed to change the SSID but not on HTC phones – xhuur Aug 29 '11 at 08:17
  • 1
    so how did you finally manage to change the SSID?? – Arno den Hond Mar 01 '12 at 13:17

2 Answers2

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);
  • Thanks for the solution. One thing i observed during development. This doesn't work with the HTC devices. – AndroGeek Aug 14 '13 at 05:02
0

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

Community
  • 1
  • 1
bbodenmiller
  • 3,101
  • 5
  • 34
  • 50