Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-sdk16-25-启用/禁用热点_Java_Android_Android Studio - Fatal编程技术网

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

Java Android-sdk16-25-启用/禁用热点,java,android,android-studio,Java,Android,Android Studio,我正在开发一款游戏,用户可以在手机/平板电脑上创建服务器,并在本地网络中托管。为了简化用户的操作,我决定启用和禁用热点,以及更改SSID和密码,但互联网上的所有代码都不起作用 例1: 例2: 例3: 我在网上什么都看不懂。虽然有很多帖子,但都有5年的历史了 如果有人能给我指出正确的方向,我会非常高兴 注: 我家里的所有设备都有android 5+,我没有机会检查这些代码是否适用于旧版本。经过几个小时的研究,我终于能够启动hotspot ** 此解决方案适用于ANDROID 6.0.1和7.0 *

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

例1:

例2:

例3:

我在网上什么都看不懂。虽然有很多帖子,但都有5年的历史了

如果有人能给我指出正确的方向,我会非常高兴

注:


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

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

**

此解决方案适用于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;
 }
问题在于写入设置权限和我的Android Studio。
我没有在危险权限列表中看到写入设置,所以我想,系统会自动授予它,而我的Android Studio不知何故没有显示所有调试信息。

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

**

此解决方案适用于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;
 }
问题在于写入设置权限和我的Android Studio。 我在危险权限列表中没有看到WRITE_设置,所以我想,系统会自动授予它,而我的Android Studio不知何故没有显示所有调试信息

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();
        }

    }

}
 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;
 }