安卓6.0.1无法通过编程方式启用WiFi热点

9
当我尝试从以下代码启用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();
            }
5个回答

3

我认为Android M不支持通过编程的方式创建热点。您可以引导用户进入设置页面,让他自己创建热点。下面的代码将帮助您前往设置页面。

  startActivity(
    new Intent(Settings.ACTION_SETTINGS));

为什么你“认为”..你有任何支持这个的东西吗?因为我正在尝试在M中启用热点。我已经创建了热点,但是我无法启用它。出现Exception in softap start java.lang.IllegalStateException: command '2707 softap set wlan0 - AkshayT

2

在您的活动中设置目标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


2

虽然这不是正确的方法,但它解决了问题。

将目标 SDK 版本更改为 21。然后即使在 Android 6.0.1 中,热点也将以编程方式启动。我认为应该有一种适用于 Android 6 及更高版本的正确方法来执行这样的过程。我认为请求运行时权限需要执行这些类型的进程。 此处介绍了 Android 运行时权限请求的方法


我之前也为蓝牙扫描做过类似的事情,将目标SDK版本降低一些,这样就可以模拟旧的功能,而且不会受到新方式的限制。 - stu

1

大家好,我尝试了各种方法,但无法在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热点(例如当我在家时就自动打开),编程的整个要点是自动化,让用户不必手动操作。Google有没有撤销这项功能的任何更改? - stu
1
@stu 我部署了那个应用程序,但我无法找到一种方法来在没有用户交互的情况下启动wifi。编程毕竟是为了自动化而不需要用户交互,但谷歌在这里占主导地位。;) - Alexander

0

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

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

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接