如何在用户拒绝后重新重新重新重新重新征用Android 12的bluetooth_scan和bluetooth_connect权限?

发布于 2025-01-30 16:36:26 字数 992 浏览 2 评论 0原文

我在针对SDK 31(Android 12)时重新征求扫描并连接到蓝牙设备所需的权限。

我在主要活动的oncreate()中称此方法为:

public void requestBluetoothPermissions() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
            || (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {

            Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");

            this.requestPermissions(new String[]{
                    Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
            }, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);

        }
    }
}

它第一次称为IE Android弹出窗口时,它可以很好地显示给用户,从而促使他授予权限。

但是,如果他拒绝授予权限,下次increate()被调用,则不会显示弹出窗口,这意味着用户仍然无法授予权限。

知道为什么以及如何解决这个问题吗?

I have a problem re-requesting the permissions required to scan and connect to bluetooth devices when targeting SDK 31 (Android 12).

I call this method inside my main activity's onCreate():

public void requestBluetoothPermissions() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
            || (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {

            Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");

            this.requestPermissions(new String[]{
                    Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
            }, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);

        }
    }
}

It's works fine the first time it is called i.e. an Android pop-up is displayed to the user, prompting him to grant the permissions.

But if he refuses to grant the permissions, next time onCreate() is called, the pop-up will not be displayed, which means the user remains unable to grant the permissions.

Any idea why and how to fix this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2025-02-06 16:36:26

仅在用户拒绝一次后,它出现Android 12个块,要求同样的权限。

因此,我最终使用activityCompat.shouldshouldshowrequestperpermissionrationale(getActivity(),subtest.permission.bluetooth_scan)确定可以请求是否请求权限,在这种情况下显示了SnackBar消息,以说明为什么会显示SnackBar消息。需要许可,并打开可以授予许可的应用程序设置的按钮。

这是代码的示例:

public void requestBluetoothPermissions() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
            || (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                    Manifest.permission.BLUETOOTH_SCAN)) {

                 // display permission rationale in snackbar message
            } else {
                Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");

                this.requestPermissions(new String[]{
                    Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
                    }, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);
            }
        }
    }
}

It appears Android 12 blocks requesting the same permission after user denied it once only.

Therefore, I ended up using ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.BLUETOOTH_SCAN) to determine wether the permission can be requested or not, in which case a snackbar message is displayed explaining why is the permission needed, with a button opening the app settings where permission can be granted.

Here is a sample of the code:

public void requestBluetoothPermissions() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        if ((this.checkSelfPermission(Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)
            || (this.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                    Manifest.permission.BLUETOOTH_SCAN)) {

                 // display permission rationale in snackbar message
            } else {
                Log.w(getClass().getName(), "requestBluetoothPermissions() BLUETOOTH_SCAN AND BLUETOOTH_CONNECT permissions needed => requesting them...");

                this.requestPermissions(new String[]{
                    Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT
                    }, MyActivity.REQUEST_BLUETOOTH_PERMISSIONS);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文