I have a project where I am connecting to a USB slave device (Android is the USB host). Before connecting however, permission is required (UsbManager.HasPermission / UsbManager.RequestPermission). Currently I have a scheme whereby permission is requested if necessary, and the user response to the Android permission request dialog is checked using the following method:
1. Set a flag that USB permission is being requested.
2. RequestPermission
3. In Activity_Resume (called when focus returns to the app after the Android permission request dialog has been handled by the user), check for the USB permission flag; if set, then check if USB permission has been granted, and thus deduce the user response to the dialog.
The above seems to work great on one device which I am testing with (phone), but not on a second device (tablet). Strangely, both devices are of the same Android version (6.0) and build (3.18.19).
Because the scheme is not working on the tablet, I am trying alternatives. One thing which I am currently trying is to use BroadCastReceiver to detect the response to the Android USB Permission Dialog (as per https://developer.android.com/guide/topics/connectivity/usb/host#using-intents, "Obtain permission to communicate with a device"). I am currently trying to implement https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/#content, however am not managing to get the "OnReceive" sub to fire. If anybody has any advice then that would be appreciated!
Here is the BroadCastReceiver code which I am currently using:
In the Android documentation "com.android.example.USB_PERMISSION" is used - I assume that replacing this with "za.dizzy.usbtest.USB_PERMISSION" is correct (where "za.dizzy.usbtest" is the package name)?
1. Set a flag that USB permission is being requested.
2. RequestPermission
3. In Activity_Resume (called when focus returns to the app after the Android permission request dialog has been handled by the user), check for the USB permission flag; if set, then check if USB permission has been granted, and thus deduce the user response to the dialog.
The above seems to work great on one device which I am testing with (phone), but not on a second device (tablet). Strangely, both devices are of the same Android version (6.0) and build (3.18.19).
Because the scheme is not working on the tablet, I am trying alternatives. One thing which I am currently trying is to use BroadCastReceiver to detect the response to the Android USB Permission Dialog (as per https://developer.android.com/guide/topics/connectivity/usb/host#using-intents, "Obtain permission to communicate with a device"). I am currently trying to implement https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/#content, however am not managing to get the "OnReceive" sub to fire. If anybody has any advice then that would be appreciated!
Here is the BroadCastReceiver code which I am currently using:
B4X:
Sub Process_Globals
Dim bcr As BroadCastReceiver
End Sub
Sub Service_Create
bcr.Initialize("bcr")
End Sub
Sub Service_Start (StartingIntent As Intent)
bcr.addAction("za.dizzy.usbtest.USB_PERMISSION")
bcr.registerReceiver("")
End Sub
Sub bcr_OnReceive(Action As String)
ToastMessageShow(Action, True)
End Sub