Android Question [SOLVED] USB Permission Popup... Every time?

Xandoca

Active Member
Licensed User
Longtime User
Hi,

Scenario: Android 7, felUsbserial and Usbmanager libraries.
Android keeps asking permission to access the usb device when: (I've select the option "use by default for this USB device")
  1. Android restart
  2. Unplug and Plug the USB
  3. Turn off and turn on the USB Device
1685886521896.png


Is there a way to configure/code that Android will not ask permission again even after a android restart and unplug and Plug the USB?

I've tried the following:
  1. I'm using Erel's example: felUsbserial
  2. Manifest Intent filters
B4X:
AddActivityText(main, <intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
       android:resource="@xml/device_filter" />)
 
AddActivityText(main, <intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
       android:resource="@xml/device_filter" />)

CreateResource(xml, device_filter.xml,
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="29987" product-id="29987" />
 
</resources>
)

Regards!!!
 
Last edited:

Xandoca

Active Member
Licensed User
Longtime User
After 2 days of reading posts/articles/android documentation and a lot of tests I could workaround the problem on Android 7.1.2. Using my mobile phone with Android 11 I didn't face the problem.

It's exactly same issue as here and here.
After android reboot, your application will not get usb permission unless you unplug and plug the usb.
You must use directBootAware so your app is "running" when Android first give/restore permissions (includind usb permission).
But with directbootaware, some time after the android reboot, app screen appears but it freezes and doesn't load the layout and it's liked the app is really freezed. But your app's services will be start (if services are configured as #StartAtBoot: true). And it was in services that I solve the problem.

Bellow the app start sequence after android reboot after solve the problem:
1) Main activity is start by android during the boot but it freezes
2) After some time (10 to 20 seconds) the booter service (StartAtBoot: true) starts and configure other service (StartApp) to start in 10 seconds (StartServiceAt). The last command in booter service is ExitApplication
3) The main activity is closed (ExitApplication)
4) After 10 seconds StartApp service starts and then start Main Activity (StartActivity(Main))
5) After that you can interact to usb device without been requested to give permission to access usb device.

Attached the final code I'm using.
You have to change the vendor-id and product-id at manifest to your usb device configuration. Also put commands to interact to your usb device (line 57 - B4XMainPage).

Special thanks to @Mihai Rainer Jr.
 

Attachments

  • usb test.zip
    90.6 KB · Views: 72
Upvote 0
Top