Android Question BroadCastReceiver and Usb RequestPermission

Discussion in 'Android Questions' started by Dane du Plooy, Nov 21, 2018.

  1. Dane du Plooy

    Dane du Plooy Member Licensed User

    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:
    Code:
    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
    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)?
     
  2. DonManfred

    DonManfred Expert Licensed User

    So you should set the Action here. What is the action your Device is Broadcasting? Try to add this action in the call to registerreceiver
     
  3. Dane du Plooy

    Dane du Plooy Member Licensed User

    I have no idea... I think that Android should broadcast something when the USB Permission dialog has been handled by the user? I'm a bit in the deep end here at the moment to be honest :-D.
     
  4. Dane du Plooy

    Dane du Plooy Member Licensed User

    I am now testing on a 2nd tablet (completely different model - Android 5.1.1, Kernel 3.10.65-7415172) and it is exhibiting the same behavior; even 2.5s (I have implemented a timeout loop) after the user has tapped "OK" in order to grant permission on the Android USB Permission dialog, UsbManager.HasPermission is still returning "False" (frustrating!!). Trying to get the BroadCastReceiver going seems like the best idea right now, to see if that perhaps works better... (?)
     
  5. Dane du Plooy

    Dane du Plooy Member Licensed User

    Looking through the Java example code at https://stuff.mit.edu/afs/sipb/proj...opics/connectivity/usb/host.html#permission-d ("Obtain permission to communicate with a device"), I see that UsbManager.RequestPermission is called with two parameters - one of them being a "PendingIntent":
    Code:
    mUsbManager.requestPermission(device, mPermissionIntent);
    UsbManager.RequestPermission in B4A only accepts one parameter however - is this sort of functionality perhaps not supported in B4A?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Dane du Plooy

    Dane du Plooy Member Licensed User

    That's pretty much what I've ended up with, thank-you, and it seems to be working much better.

    I've added a dummy activity to act as the target for the intent, which just closes as soon as it is run, because I do not actually want to jump to a specific activity when the USB device is inserted.

    The static intent filter seems like a much better option - once permission has been granted it is not asked for again, whereas with the runtime permission it is asked for every time the device is removed and inserted (except on the one phone I have here, which never seems to ask for permission ever - weird).

    I have manually created the usb device xml file in the manifest:
    Code:
    CreateResource(xml, usb_device_filter.xml,
    <resources>
      <usb-device vendor-
    id="1234" product-id="5678" />
    </resources>
    )
     
    Erel likes this.
  8. DonManfred

    DonManfred Expert Licensed User

    create a service and configure the intent filter to start this service (do not use the starter service for this). No need to open an activity. even if it just quits as it runs.
     
  9. Dane du Plooy

    Dane du Plooy Member Licensed User

    Thanks Manfred :).
    I think I read somewhere that the USB intent has to target an Activity for some reason, rather than a service. A little experimenting would confirm that.
     
  10. DonManfred

    DonManfred Expert Licensed User

    ohh, ok. Didn´t know that.
    That´s true. Worth a try.
     
  11. Dane du Plooy

    Dane du Plooy Member Licensed User

    From https://stackoverflow.com/questions...de-service-start-based-on-usb-device-attached

    I'm pressed for time at the moment, but if I have a gap then I'll try it out myself to see if a service indeed doesn't work :).
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice