如何在用户拒绝后重新重新重新重新重新征用Android 12的bluetooth_scan和bluetooth_connect权限?
我在针对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅在用户拒绝一次后,它出现Android 12个块,要求同样的权限。
因此,我最终使用
activityCompat.shouldshouldshowrequestperpermissionrationale(getActivity(),subtest.permission.bluetooth_scan)
确定可以请求是否请求权限,在这种情况下显示了SnackBar消息,以说明为什么会显示SnackBar消息。需要许可,并打开可以授予许可的应用程序设置的按钮。这是代码的示例:
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: