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.