views:

742

answers:

4

What is the API call I need to make in Android 2.2 (Froyo) to create a Wifi hotspot (as seen in the Tethering and Portable Hotspot settings item).

A: 

There does not appear to be an API call to create a WiFi hotspot -- sorry!

CommonsWare
+4  A: 

You can call public 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

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!

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?

markov00
good investigation
Schildmeijer
You can always access them through reflection!
Chris Thompson
@markov00: Your suggested approach is not part of the Android SDK. Do not use it, period.
CommonsWare
@CommonsWare, why not? It might not work in a future version of Android, I suppose, but if you're willing to take that risk, what's wrong with that?
MatrixFrog
@MatrixFrog: Tactically, it may break on current devices. Device manufacturers are very willing to remove or alter this feature on behalf of carriers. Assuming that any non-SDK code will work, or even exist, is simply not reliable. If you want to use this code for some toy app you use on your own phone, be my guest. Only a fool will ship an application, though, that is designed to disappoint customers. Strategically, the more developers go past the SDK, the more difficult it is for me and others to convince manufacturers to not screw with stuff that's supposed to be supported in the SDK.
CommonsWare
@CommonsWare: For embedded applications, it is perfectly ok to use a specific firmware and a specific device, and not publish the application on an app market.
Lars D
I might also go with this approach (since I bundle my app with a device and am in full control of what version runs on the device). The SDK limits it's API often for no good reason - there are already many example as discussed on the Google Android dev group. Unfortunately reflection then is the only way to handle it, and I'd say go with it as long as it works for your own specific use-case.
Mathias Lin
@markov00: is that working for your case? I tried above on SGS 2.1, but there's no method 'setWifiApEnabled' found in the loop, only found setWifiEnabled (not Ap), showApDialog, setEnabledApDialog. Another thing: How do you get/set the key? Is that in netConfig.wepKeys[0] after you invoked the method?
Mathias Lin
A: 

Hi All,

I'm a newbie on Android. Please tell me what APIs to build applications that make my Android device (Nexus one) to tether or can use it as a mobile hotspot.

If there is no API that support to do this task. Please tell me what technique/mechanism exits at this point to manage all connections (by my own program) from "Portable Wi-Fi Hotspot". Where "Portable Wi-Fi Hotspot" is described in the link below:

http://techcrunch.com/2010/05/13/exclusive-google-to-add-tethering-wifi-hotspot-to-android-2-2-froyo/

Please help me. Thank you. Please forgive for my bad english !

Nguyen Dai Son
If you have a question, ask it instead of commenting on other's questions. Thanks.
Lars D
A: 

Hi CommonsWare,

Thanks for your quick answer.

Please tell me how to approach to the solution, such as from Andriod NDK, Linux Kernel,...to
solve my problem. Please help me. Thank you!

Please forgive for my bad english!

Nguyen Dai Son