0

我想以编程方式打开wifi热点。我使用以下代码:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void enableHotspot(View v){
        WifiAccessManager.setWifiApState(this, true);

    }
}

public class WifiAccessManager {

    private static final String SSID = "1234567890abcdef";
    public static boolean setWifiApState(Context context, boolean enabled) {
        //config = Preconditions.checkNotNull(config);
        try {
            WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (enabled) {
                mWifiManager.setWifiEnabled(false);
            }
            WifiConfiguration conf = getWifiApConfiguration();
            mWifiManager.addNetwork(conf);

            return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static WifiConfiguration getWifiApConfiguration() {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID =  SSID;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        return conf;
    }
}

AndroidmManifest 文件中的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我正在 2 个不同的设备(android 4 和 android 6)上对其进行测试。这在 android 4 上正常工作(没有问题),但在 android 6 上它不起作用,并且正在生成以下日志:

java.lang.reflect.InvocationTargetException
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at ir.captainweb.wifitests.WifiAccessManager.setWifiApState(WifiAccessManager.java:20)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at ir.captainweb.wifitests.MainActivity.enableHotspot(MainActivity.java:17)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.view.View.performClick(View.java:5210)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.view.View$PerformClick.run(View.java:21328)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5551)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: Caused by: java.lang.SecurityException: ir.captainweb.wifitests was not granted  this permission: android.permission.WRITE_SETTINGS.
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Parcel.readException(Parcel.java:1599)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.os.Parcel.readException(Parcel.java:1552)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1540)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:     at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1588)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err:  ... 14 more

我还添加了 WRITE_SETTINGS 权限,但我又遇到了同样的问题

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
4

1 回答 1

0

我们必须在 android 6 中手动授予权限。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  // please check here whether you already have these permission or not.
        requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 2909);
    }

无论是否授予,您都会得到许可的响应。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 2909: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e("Permission", "Granted");
            } else {
                Log.e("Permission", "Denied");
            }
            return;
        }
    }
}
于 2017-09-02T19:16:13.673 回答