1

I'm trying to run a hotspot with a new name and open accessibility.

    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wifiConfig.SSID = "\"MySSID\"";
    wifiConfig.networkId = 1;
    methodNum = getMethodNumber("setWifiApEnabled");
    try {
        wmMethods[methodNum].invoke(wifiManager, wifiConfig, true);
    } catch (IllegalArgumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InvocationTargetException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }        

I get the right method and it seems like it starts the hotspot on the phone, but the configuration doesn't change.

I tried to get the current configuration data with getWifiApConfiguration and i get nothing with it, no ssid and not the current encryption.

I'm using HTC Evo 3d for the debugging.

animuson
  • 53,861
  • 28
  • 137
  • 147
user1624426
  • 63
  • 3
  • 10
  • Those are the permissions I've tried: ACCESS_WIFI_STATE. UPDATE_DEVICE_STATS, CHANGE_WIFI_STATE, UPDATE_DEVICE_STATS. CHANGE_NETWORK_STATE. WRITE_SETTINGS – user1624426 Oct 10 '12 at 17:12
  • @user1624426 are you trying to connect to wifi with that configuration or instead you want to enable your phone to be the tethering (hotspot) device? – gumuruh Aug 17 '14 at 06:43

2 Answers2

4

Some htc phones seems to use a class of type HotspotProfile to keep its configuration. So, before calling setWifiApEnabled, you need set the ssid in htc's way:

if (isHtc) setHTCSSID(config);
methodNum = getMethodNumber("setWifiApEnabled");
try {
    wmMethods[methodNum].invoke(wifiManager, wifiConfig, true);
    ...

isHtc can be calculated like:

 try { isHtc = null!=WifiConfiguration.class. getDeclaredField("mWifiApProfile");  }
 catch (java.lang.NoSuchFieldException e) { isHtc = false }

and setHTCSSID would be:

public void setHTCSSID(WifiConfiguration config) {
    try {
        Field mWifiApProfileField = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
        mWifiApProfileField.setAccessible(true);
        Object hotSpotProfile = mWifiApProfileField.get(config);
        mWifiApProfileField.setAccessible(false);

        if(hotSpotProfile!=null){
            Field ssidField = hotSpotProfile.getClass().getDeclaredField("SSID");
            ssidField.setAccessible(true);
            ssidField.set(hotSpotProfile, config.SSID);
            ssidField.setAccessible(false);

        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

I found this information in some chinese blogs: http://xiaxingwork.iteye.com/blog/1727722 and http://blog.sina.com.cn/s/blog_53dd443a010109i8.html

Luis
  • 1,282
  • 1
  • 11
  • 25
  • hi @luis i want to create hot spot in my htc phone and i create that but i cannot connect to my hotspot its going to config ip address but never going to done my full question is here http://stackoverflow.com/questions/29326985/set-ssid-for-hotspot-in-android-4-0 can you help me please? – max Mar 29 '15 at 10:48
1

It seems like its a problem with HTC. Some of my friends tried similar code on HTC and other devices. Didnt work on HTC, worked on the others.

user1624426
  • 63
  • 3
  • 10