Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for permission state. #944

Open
infacto opened this issue Nov 22, 2022 · 5 comments
Open

Add method for permission state. #944

infacto opened this issue Nov 22, 2022 · 5 comments

Comments

@infacto
Copy link

infacto commented Nov 22, 2022

Should we add a method for the new Android 12+ permissions?

Method Description
isEnabled Reports if bluetooth is enabled.
isLocationEnabled Reports if location services are enabled.
isScanEnabled Reports if bluetooth can scan. (Android 12, API 31) new

How to test permission BLUETOOTH_SCAN? How to test if the bluetooth plugin is fully ready?
Is it possible to test the permission before using it?

My test: On Android 12 you only get the permission dialog two times. When you get the error "Bluetooth scan permission not granted." Btw. before this error the app event platform.pause is emitted and after this error platform.resume. That's weird. Another thing is, I have no idea where to enable BLUETOOTH_SCAN in Android settings. I cannot find it. Currently I have to uninstall the app and install again to trigger the permission dialog. We need a way to trigger this again or open settings if the user must manually enable it.

Btw. Are all errors always typeof string? For example on startScanWithOptions? Because we generally expect Error object. I could test string if includes scan and permission to handle this kind of error. ^^

Ref. #875

@peitschie
Copy link
Collaborator

peitschie commented Nov 22, 2022

Hi @infacto, agree this would be a useful feature to have. It's not currently possible to check the permissions using this plugin on it's own.

In my current applications I've been using the cordova-diagnostic-plugin to support this workflow, but it's certainly a pain having to keep these permission checks aligned with the plugin as well, especially given the permissions are different for each Android release.

@peitschie
Copy link
Collaborator

My test: On Android 12 you only get the permission dialog two times. When you get the error "Bluetooth scan permission not granted."

Yep. This varies between phone manufacturers, making Android extra fun.

Btw. before this error the app event platform.pause is emitted and after this error platform.resume. That's weird.

This is due to Android's behaviour when the permission dialog appears. The permission dialog effectively pauses the requesting app until the permission is granted/denied, at which point the app resumes.

Another thing is, I have no idea where to enable BLUETOOTH_SCAN in Android settings. I cannot find it. Currently I have to uninstall the app and install again to trigger the permission dialog. We need a way to trigger this again or open settings if the user must manually enable it.

On Android 12, this setting is called Nearby devices, and can be found under the normal App permissions. You can tap on this permission and deny it to reset the prompt again for another test.

@infacto
Copy link
Author

infacto commented Nov 22, 2022

Ok thanks, I found the permission now. Another issue is: You can disable location at any time. For example pull down the Android control center and tap on location. Then call this ble plugin startScanWithOptions and see that no error appears. It behaves like just no device found. (Update: registerLocationStateChangeHandler form diagnostic plugin might help.)

@peitschie
Copy link
Collaborator

Though most phones do require location services enabled to scan, not every phone does. I haven't yet figured out a good solution to tell whether the scan works or not, as Android doesn't return any error, it just ignored the scan request silently.

More discussion in this can be found here: #872

@infacto
Copy link
Author

infacto commented Nov 22, 2022

Just another idea / think aloud: If possible we could implement a method to open permission setting (where is e.g. "nearby devices") for this app. Like the method showBluetoothSettings. Not tested, but I see that the diagnostic plugin has a method switchToSettings. Maybe we could use that. Currently I have to figure out how to implement the best UX for Luser for all Android version (7+) and its quirks. The diagnostics plugin might help.

cordova.plugins.diagnostic.getPermissionAuthorizationStatus(
  (status) => console.log('Status:', status), 
  (error) => console.error('Error:', error), 
  'BLUETOOTH_SCAN'
)
// Result in case the permission was denied by the user and the dialog is never shown again: 
// Status: DENIED_ALWAYS (btw. NOT_REQUESTED is also possible. Read more in docs.)

docs

Btw. it seems that there are two DENIED state. ONCE and TWICE. So it's intended from Android. When the user denied twice, the dialog does not appear anymore. But idk. if different devices acts in different way. ...

Update: I think getBluetoothAuthorizationStatuses is the better choice. Or just getBluetoothAuthorizationStatus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants