开启WiFi热点的代码类 这个异常应该是 invoke()抛出的
为测试我是自己mainactivity 的oncreate()里面调用createAp的
package com.example.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.widget.Toast;
public class WifiApAdmin{
private WifiManager wifi;
private String ssid="test";
private String pass="123456";
public WifiApAdmin(Context context){
wifi=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
}
public void createAp(Activity activity){
if(wifi.isWifiEnabled()){
wifi.setWifiEnabled(false);
}
WifiConfiguration ap=new WifiConfiguration();
ap.SSID=ssid;
ap.preSharedKey=pass;
ap.hiddenSSID=true;
ap.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
ap.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
ap.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
ap.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
ap.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
ap.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
ap.status=WifiConfiguration.Status.ENABLED;
try{
Method method=wifi.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,Boolean.TYPE);
boolean enable=(Boolean)method.invoke(wifi,ap,true);
if(enable){
//成功
Toast.makeText(activity, "成功", Toast.LENGTH_SHORT).show();
}else{
//失败
Toast.makeText(activity, "失败", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
5 个解决方案
#1
你的问题解决了吗?我也碰到这样的问题了,我起先以为是权限问题,但是设置了还是不对
#2
这个问题困扰好久,没有大神遇到吗。急问
#3
好久 发帖子了 好久发的问题了 现在都忘记 怎么解决的了 看看先
#4
好久了 看一下代码 有点印象 好像是 开启热点的设置参数设置 不对 不知道后来怎么解决的 好像 也是改参数就好 唉 以后每解决一个bug 应该写注释 记录一下
#5
Method enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration myConfig = new WifiConfiguration();
myConfig.SSID = ssid ;
myConfig.preSharedKey = password ;
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);
另外加上权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
#1
你的问题解决了吗?我也碰到这样的问题了,我起先以为是权限问题,但是设置了还是不对
#2
这个问题困扰好久,没有大神遇到吗。急问
#3
好久 发帖子了 好久发的问题了 现在都忘记 怎么解决的了 看看先
#4
好久了 看一下代码 有点印象 好像是 开启热点的设置参数设置 不对 不知道后来怎么解决的 好像 也是改参数就好 唉 以后每解决一个bug 应该写注释 记录一下
#5
Method enableWifi = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration myConfig = new WifiConfiguration();
myConfig.SSID = ssid ;
myConfig.preSharedKey = password ;
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);
另外加上权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />