gpt4 book ai didi

Android Lollipop - WiFi 热点 setWifiApEnabled() 获取 InvocationTargetException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:08 25 4
gpt4 key购买 nike

我在应用中使用

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但这是一个有点旧的代码。

你有这方面的经验吗?你有什么建议?

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

最佳答案

试试这个。

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;
}

如果出现异常,请尝试将此权限添加到您的 list 中android.permission.WRITE_SETTINGS

关于Android Lollipop - WiFi 热点 setWifiApEnabled() 获取 InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30527188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com