【问题标题】:Android 6.0.1 couldn't enable wifi hotspot programmaticallyAndroid 6.0.1 无法以编程方式启用 wifi 热点
【发布时间】:2016-03-25 03:51:06
【问题描述】:

当我尝试通过以下代码启用 wifi 网络共享时,它会引发异常

java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.....

....未授予此权限:android.permission.WRITE_SETTINGS

但这在 android 6.0 及以下版本中运行良好。并且还尝试了提供 android.permission.WRITE_SETTINGS。

android 6.1访问wifiAP有什么限制吗?

按照我附上我用来启用热点的代码示例。

            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = ssId;
            netConfig.preSharedKey = passkey;
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            
            try {
                boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);
                
                for (Method isWifiApEnabledmethod : wmMethods) {
                    if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                        while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {}

                        for (Method method1 : wmMethods) {
                            if (method1.getName().equals("getWifiApState")) {
                                int apstate;
                                apstate = (Integer) method1.invoke(wifiManager);
                                Log.i(TAG, "Apstate ::: " + apstate);
                            }
                        }
                    }
                }
                
                if (apstatus) {
                    Log.d(TAG, "Access Point created");
                } else {
                    Log.d(TAG, "Access Point creation failed");
                }

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

【问题讨论】:

    标签: android android-wifi android-6.0-marshmallow android-permissions


    【解决方案1】:

    我认为 Android M 不支持以编程方式创建热点。您可以将您的 Marshmallow 用户带到设置页面以自行创建热点。下面的代码将帮助你去设置页面。

      startActivity(
        new Intent(Settings.ACTION_SETTINGS));
    

    【讨论】:

    • 你为什么要“思考”..你有什么支持这一点的吗?因为我试图在 M 中启用热点。我已经创建了热点,但我无法启用它。获取Exception in softap start java.lang.IllegalStateException: command '2707 softap set wlan0
    【解决方案2】:

    这不是正确的方法。但这解决了问题。

    将目标 sdk 版本更改为 21。即使在 android 6.0.1 中,热点也会以编程方式启动。认为对于 android 6 及更高版本应该有适当的方法来执行此操作。我认为请求运行时权限需要执行这些进程。 This talks about the android permission requesting in runtime

    【讨论】:

    • 我不得不为蓝牙扫描做一些类似的事情,将目标 sdk 设置回来一点,它会模拟旧的功能,并且不会妨碍你使用新的做事方式。
    【解决方案3】:

    在您的活动中设置目标SDK 版本21 并请求write_settings 许可。还要在清单中添加android.permission.WRITE_SETTINGS 权限。

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_SETTINGS)){
    
    }else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_SETTINGS},
                121);
    }
    

    更多详情请访问http://developer.android.com/training/permissions/requesting.html

    【讨论】:

      【解决方案4】:

      各位,我尝试了所有方法,但无法在 Android 6.0 中启动热点。 您可以检查 Api 是否 >= 23,如果是,则将用户带到设置页面自行创建热点。

      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          final Intent intent = new Intent(Intent.ACTION_MAIN, null);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          final ComponentName cn = new ComponentName(
                         "com.android.settings",
                         "com.android.settings.TetherSettings");
          intent.setComponent(cn);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity( intent);
      }else{
         createhotSpot();
      }
      

      【讨论】:

      • 我写了一个应用程序,它可以根据位置服务打开 wifi 热点(例如,当我在家时它会继续)编程的全部意义在于使事情自动化,这样用户就不会不必做任何事情。谷歌取消了该功能的任何更改?
      • @stu 我部署了那个应用程序,但我无法找到在没有用户交互的情况下如何启动 wifi 的方法。是的,编程毕竟是一种无需用户交互的自动化,但谷歌在这里决定了游戏;)
      【解决方案5】:

      权限不是您的问题。你需要这样的代码:

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      if (!Settings.System.canWrite(getApplicationContext())) {
          Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
          startActivityForResult(intent, 200); //You need a callback for activity result so that check if user enabled this feature, go for starting hotspot (google for it)
      } else {
          // Do your stuff about starting hotspot (in network thread)
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        • 2011-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多