Android 6.0.1无法以编程方式启用wifi热点 [英] Android 6.0.1 couldn't enable wifi hotspot programmatically

查看:233
本文介绍了Android 6.0.1无法以编程方式启用wifi热点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

java.lang.reflect.Method.invoke(本机方法)处的java.lang.reflect.InvocationTargetException.

....未被授予此权限: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 M不支持以编程方式创建热点.您可以将您的棉花糖用户带到设置页面以自己创建热点.下面的代码将帮助您进入设置页面.

  startActivity(
    new Intent(Settings.ACTION_SETTINGS));

When I tried to enable wifi tethering from the following code it throws the exception

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

.... not granted this permission: android.permission.WRITE_SETTINGS

But this works fine in android 6.0 and below versions. And also tried with giving android.permission.WRITE_SETTINGS too.

Is there any limitation in accessing wifiAP in android 6.1?

Follow I attached the code sample that I used to enable hotspot.

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

解决方案

I think Android M don't support to create hotspot programmatically . You can take your Marshmallow user to settings page to create hotspot by himself. below code will help yo to go setting page.

  startActivity(
    new Intent(Settings.ACTION_SETTINGS));

这篇关于Android 6.0.1无法以编程方式启用wifi热点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆