2015-12-09 42 views
7

我試過使用以下代碼在Android Marshmallow中創建wifi tethering Hotspot。如何在Android Marshmallow中創建WiFi tethering Hotspot?

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. 

但我已經添加了清單。 。

我該如何解決問題?

回答

-1

我認爲Android M不支持以編程方式創建熱點。您可以將棉花糖用戶帶到設置頁面,自行創建熱點。下面的代碼將幫助你去設置頁面。

startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)); 
1

我在Android棉花糖工作,並找到了創建WiFi共享的方法,如下所述。請注意,根據Android 6.0 Changes只有創建這些對象時,您的應用才能更改WifiConfiguration對象的狀態。從Android 6.0(API級別23)開始,用戶在應用程序運行時嚮應用程序授予權限,而不是在安裝應用程序時授予應用程序權限。 Read this article to know more about this.我可以看到你正在創建自己的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棉花糖圈養熱點:

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

啓用熱點函數調用:setWifiTetheringEnabled(true)和禁用setWifiTetheringEnabled(false)

就是這樣。

N.B.請注意,不支持SIM的設備不支持使用熱點。您將無法在沒有root的設備上創建熱點。

希望這將有助於即將到來的訪問者。

相關問題