如何在Android Marshmallow中创建wifi网络共享热点?

时间:2015-12-09 06:46:48

标签: android android-wifi android-6.0-marshmallow tethering

我尝试使用以下代码在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 个答案:

答案 0 :(得分:1)

我在Android Marshmallow工作,并找到了创建WiFi网络共享的方法,如下所述。请注意,根据Android 6.0 Changes您的应用程序现在只有在您创建这些对象时才能更改WifiConfiguration对象的状态。从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。 Read this article to know more about this.我可以看到你正在创建自己的Hotspot。所以没问题。 Manifest的许可如下:

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

启用热点功能调用是:setWifiTetheringEnabled(true)和禁用setWifiTetheringEnabled(false)

那就是它。

<强> N.B。请注意,不支持SIM卡设备使用Hotspot。如果没有root,您将无法在这些设备上创建热点。

希望这对即将到来的访客有所帮助。