【问题标题】:How to create wifi tethering Hotspot in Android Marshmallow?如何在 Android Marshmallow 中创建 wifi 网络共享热点?
【发布时间】:2015-12-09 06:46:48
【问题描述】:

我尝试使用以下代码在 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 android-wifi android-6.0-marshmallow tethering


    【解决方案1】:

    我在 Android Marshmallow 中工作,并找到了一种创建 WiFi 网络共享的方法,如下所述。请注意,根据Android 6.0 Changes,您的应用程序现在可以更改 WifiConfiguration 对象的状态,前提是您创建了这些对象。从 Android 6.0(API 级别 23)开始,用户在应用运行时授予应用权限,而不是在安装应用时。 Read this article to know more about this.我可以看到您正在自己创建热点。所以没有问题。 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;
            }
        }
    }
    

    启用 Hotspot 函数调用是:setWifiTetheringEnabled(true) 和禁用 setWifiTetheringEnabled(false)

    就是这样。

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

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

    【讨论】:

    • WRITE_SETTINGS 仅适用于系统应用程序,我们该怎么做?
    • 是的,@Manny265,你是对的。它允许应用程序读取或写入系统设置。请阅读此链接developer.android.com/reference/android/…中的详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多