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

TKu*_*mar 6 android android-wifi tethering android-6.0-marshmallow

我尝试使用以下代码在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

}

但它显示以下权限问题..

java.lang.SecurityException: googleplus.tarun.info.hotspotcreation was not granted  either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS.
Run Code Online (Sandbox Code Playgroud)

但我已经在清单上添加了那些..

我该如何解决这个问题?

Sid*_*oor 1

我在 Android Marshmallow 中工作,并找到了一种创建 WiFi 网络共享的方法,如下所述。请注意,根据Android 6.0 更改,仅当您创建了 WifiConfiguration 对象时,您的应用程序现在才可以更改这些对象。从 Android 6.0(API 级别 23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时授予权限。阅读本文以了解更多相关信息。我可以看到您正在自己创建热点。所以没问题。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"/>
Run Code Online (Sandbox Code Playgroud)

我正在使用以下函数在 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

启用热点的函数调用是:setWifiTetheringEnabled(true)和禁用setWifiTetheringEnabled(false)

就是这样。

注意:请注意,无 SIM 卡设备不支持使用热点。如果没有 root,您将无法在这些设备上创建热点。

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