![](/img/trans.png)
[英]How to create the wifi tethering without sharing the internet connection(Hotspot) in android?
[英]How to create wifi tethering Hotspot in Android Marshmallow?
我嘗試使用以下代碼在Android Marshmallow中創建Wi-Fi網絡共享熱點。
public class WifiAccessManager {
private static final String SSID = "1234567890abcdef";
public static boolean setWifiApState(Context context, boolean enabled) {
//config = Preconditions.checkNotNull(config);
try {
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (enabled) {
mWifiManager.setWifiEnabled(false);
}
WifiConfiguration conf = getWifiApConfiguration();
mWifiManager.addNetwork(conf);
return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static WifiConfiguration getWifiApConfiguration() {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = SSID;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
return conf;
}
}
但它顯示以下權限問題:
java.lang.SecurityException: googleplus.tarun.info.hotspotcreation was not granted either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS.
即使我已經在清單上添加了那些。
我該如何解決這個問題?
我在Android Marshmallow工作,並找到了一種創建WiFi網絡共享的方法,如下所述。 請注意,根據Android 6.0更改您的應用程序現在只有在您創建這些對象時才能更改WifiConfiguration對象的狀態。 從Android 6.0(API級別23)開始,用戶在應用程序運行時向應用程序授予權限,而不是在安裝應用程序時。 閱讀本文以了解更多相關信息。 我可以看到你正在創建自己的Hotspot。 所以沒問題。 清單中的許可如下:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
我使用以下函數在android marshmallow中創建WiFi網絡共享熱點:
public void setWifiTetheringEnabled(boolean enable) {
//Log.d(TAG,"setWifiTetheringEnabled: "+enable);
String SSID=getHotspotName(); // my function is to get a predefined SSID
String PASS=getHotspotPassword(); // my function is to get a predefined a Password
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
if(enable){
wifiManager.setWifiEnabled(!enable); // Disable all existing WiFi Network
}else {
if(!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(!enable);
}
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
WifiConfiguration netConfig = new WifiConfiguration();
if(!SSID.isEmpty() || !PASS.isEmpty()){
netConfig.SSID=SSID;
netConfig.preSharedKey = PASS;
netConfig.hiddenSSID = false;
netConfig.status = WifiConfiguration.Status.ENABLED;
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
}
try {
method.invoke(wifiManager, netConfig, enable);
Log.e(TAG,"set hotspot enable method");
} catch (Exception ex) {
}
break;
}
}
}
啟用Hotspot函數調用是: setWifiTetheringEnabled(true)
和disable setWifiTetheringEnabled(false)
。
而已。
注意請注意,不支持SIM卡設備使用Hotspot。 您將無法在沒有root的情況下在這些設備上創建Hotspot。
希望這對即將到來的訪客有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.