【问题标题】:Android - sdk16-25 - Enable/Disable HotspotAndroid - sdk16-25 - 启用/禁用热点
【发布时间】:2017-01-07 19:14:49
【问题描述】:

我正在开发一款游戏,用户将能够在手机/平板电脑上创建服务器并将其托管在本地网络中。为了简化用户操作,我决定处理启用和禁用热点以及更改 SSID 和密码,但是在 Internet 上找到的代码都不起作用。

示例 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+,我没有机会检查该代码是否适用于旧版本。

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

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

    **

    此解决方案适用于 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 不知何故没有显示所有调试信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 2017-03-04
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多