Android Lollipop - WiFi Hotspot setWifiApEnabled()获取InvocationTargetException

时间:2015-05-29 10:24:37

标签: android android-5.0-lollipop android-wifi wifimanager

我在应用中使用

setWifiApEnabled()
来自隐藏API的

(通过反射访问)。它在一些较旧的手机上工作(还有三星Galaxy S3,有些手机有4.4,......)但我用三星Galaxy S5测试了它,我得到了

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
...

我使用的代码类似于Android 2.2 wifi hotspot APIHow and what to set to Android WifiConfiguration.preSharedKey to connect to the WPA2 PSK WiFi network,但它有点旧代码。

你有经验吗?你有什么建议?

P.S。它在某些Android 4.4设备中也不起作用(但我没有得到InvocationTargetException)。

5 个答案:

答案 0 :(得分:3)

试试这个。

ConnectivityManager cman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Method[] methods = cman.getClass().getMethods();

        try
        {
            wifiManager.setWifiEnabled(false);
            Method enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            String ssid  =   " " //your SSID 
            String pass  =   " " // your Password
            WifiConfiguration  myConfig =  new WifiConfiguration();
            myConfig.SSID = ssid;
            myConfig.preSharedKey  = pass ;
            myConfig.status = WifiConfiguration.Status.ENABLED;
            myConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            myConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            myConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
            myConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
            myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
            myConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            result = (Boolean) enableWifi.invoke(wifiManager, myConfig, true);

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            result = false;
        }

如果您遇到异常,请尝试将此权限添加到您的清单中 android.permission.WRITE_SETTINGS

答案 1 :(得分:3)

我遇到了同样的问题。

android.permission.WRITE_SETTINGS添加到清单文件中并且有效。试试这个。

答案 2 :(得分:3)

您需要将这些权限添加到AndroidManifest.xml:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

它对我有用。

答案 3 :(得分:1)

我找到了原因:

Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10260 nor current process has android.permission.CONNECTIVITY_INTERNAL.

的信息:

How can system app located in /system/app have system permission in Android 4.4 KitKat build?

这是一个大问题:)

答案 4 :(得分:1)

public void setWiFiApMode(boolean mode) {
        if (mContext == null) return;
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager == null) return;
        try {
            Method setWifiApEnabled = WifiManager.class.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            setWifiApEnabled.invoke(wifiManager, null, mode);
        } catch (Exception e) {
        }
    }

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

完全适用于Android N.