techres / AndroidWifi / [z]How to enable hotspot by api

分享

   

[z]How to enable hotspot by api

2012-06-20  techres

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).

link|improve this question
I still can't believe there is no API for activating/deactivating tethering. Hope that it exists, but is just not documented yet. – Sney Jul 2 '10 at 13:04
feedback

2 Answers

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?

link|improve this answer
good investigation – Schildmeijer Aug 2 '10 at 14:55
You can always access them through reflection! – Chris Thompson Aug 2 '10 at 14:56
@markov00: Your suggested approach is not part of the Android SDK. Do not use it, period. – CommonsWare Aug 26 '10 at 3:52
@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 Aug 27 '10 at 20:06
2  
@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 Aug 29 '10 at 7:41
show 5 more comments
feedback

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

link|improve this answer

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多
    喜欢该文的人也喜欢 更多

    ×
    ×

    ¥.00

    微信或支付宝扫码支付:

    开通即同意《个图VIP服务协议》

    全部>>