且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android棉花糖中创建wifi网路共享热点?

更新时间:2023-01-25 18:08:29

我正在使用Android Marshmallow,并找到了一种创建WiFi网络共享的方法,如下所述.请注意,根据 Android 6.0更改现在,只有创建了这些对象,您的应用程序才能更改WifiConfiguration对象的状态.从Android 6.0(API级别23)开始,用户在应用程序运行时(而不是在安装应用程序时)授予应用程序权限. 阅读本文以了解有关此内容的更多信息.我可以看到您正在创建Hotspot由您自己.所以没问题.清单中的权限如下:

I was working in Android Marshmallow and have found a way to create WiFi tethering as describe below. Note that according to Android 6.0 Changes Your apps can now change the state of WifiConfiguration objects only if you created these objects. And beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. Read this article to know more about this. I can see you are creating Hotspot by your own. So no issue. The permission in Manifest is given below:

<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棉花糖中创建WiFi绑定热点:

I am using the following function to create WiFi tethering Hotspot in android marshmallow:

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).

Enabling the Hotspot the function call is: setWifiTetheringEnabled(true) and for disable setWifiTetheringEnabled(false).

就是这样.

N.B.请注意,不支持使用SIM卡少的设备来使用Hotspot.没有root用户,您将无法在这些设备上创建热点.

希望这对即将到来的访客很有帮助.

Hope this will be helpful for upcoming visitors.