Android - sdk16-25 - 启用/禁用热点

时间:2017-01-05 11:30:32

标签: java android android-studio

我正在制作一款游戏,用户可以在手机/平板电脑上创建服务器并将其托管在本地网络中。为了简化用户的工作,我决定处理启用和禁用热点以及更改SSID和密码,但在互联网上找不到任何代码。

示例1:

public void EnableHotspot3()
{


    try {
        WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

            mWifiManager.setWifiEnabled(false);

        WifiConfiguration conf = getWifiApConfiguration();
        mWifiManager.addNetwork(conf);

         mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, true);
    } catch (Exception e) {
        e.printStackTrace();

    }

}

public static WifiConfiguration getWifiApConfiguration() {
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID =  "DupaHotspot";
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    return conf;
}

示例2:

public void EnableHotspot2()
{
    if(!this.isHotspotON())
    {
        WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = "PartyGames";
        wifiConfig.preSharedKey = "4444";
        wifiConfig.hiddenSSID = false;
        wifiConfig.allowedKeyManagement.set((WifiConfiguration.KeyMgmt.WPA_PSK));
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        try{
            Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
            method.invoke(wifiMgr,wifiConfig,false);
            wifiMgr.saveConfiguration();
        }
        catch (Exception e)
        {
            e.getMessage();
        }

    }

}

示例3:

public boolean isHotspotON(){
    WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    try{
        Method method = wifiMgr.getClass().getDeclaredMethod("isWifiApEnabled");
        method.setAccessible(true);
        return (Boolean) method.invoke(wifiMgr);
    }
    catch(Throwable ignoreException)
    {
        return false;
    }
}

public void EnableHotspot()
{
    if(!this.isHotspotON())
    {
        WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wifiConfig = new WifiConfiguration();
        try{

            Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
            method.invoke(wifiMgr,wifiConfig,!isHotspotON());

            }
        catch(Exception e) {
                e.getStackTrace();
        }

    }

}

我无法在互联网上做任何事情。有很多帖子,但就像5岁一样。

如果有人能指出我正确的方向,我会很高兴。

注意:

我在家里使用的所有设备都有android 5+,我没有机会检查该代码是否适用于旧版本。

1 个答案:

答案 0 :(得分:0)

经过几个小时的研究,我终于能够开始热点......

**

  

此解决方案适用于ANDROID 6.0.1和7.0。

** **

  

未在旧版本上测试。

**

这是我的代码:

 public boolean ToggleHotspot(boolean ON_OFF)
{



    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {


        if (!Settings.System.canWrite(mContext))
        {
            Intent writeSettingIntent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);

          // Works when calling directly from MainActivity.java,but not from Android Library.WHY??
          // writeSettingIntent.setData(Uri.parse("package: " + mContext.getPackageName()));
            writeSettingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(writeSettingIntent);
        }
    }
        //TODO -> Check if hotspot enabled.If not start it...
         WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                try{

                    if(this.isWiFiEnabled())
                    {
                        this.DisableWiFi();
                    }

                    Method invokeMethod = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
                    return (Boolean)invokeMethod.invoke(wifiMgr,initHotspotConfig(),ON_OFF);
                }
                catch(Throwable ignoreException)
                {
                    return false;
                }


}




 private WifiConfiguration initHotspotConfig(){

     WifiConfiguration wifiConfig = new WifiConfiguration();

      wifiConfig.SSID = "Test Hotspot";
     // must be 8 length
      wifiConfig.preSharedKey = "abcd1234";

      wifiConfig.hiddenSSID = true;


     wifiConfig.status = WifiConfiguration.Status.ENABLED;
     wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
     wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
     wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
     wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
     wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
     wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);


    return wifiConfig;
 }

问题在于WRITE_SETTINGS权限和我的Android Studio。 我没有在危险权限列表中看到WRITE_SETTINGS所以我想,系统会自动授予它,而我的Android Studio在某种程度上没有显示所有调试信息。