Android Question felUSBSerial - boot problem

Mihai Rainer Jr.

Licensed User
Hi, I'm trying to write an application that starts based on intent-filter related to an USB to serial converter.
I'm trying to execute it on an industrial panel with Android 7.1.2
Everything is working fine, the app is starting when the USB cable is connected, on the first run is asking for permission and if the checkbox is checked, it "remembers" the permission after boot.
The only problem I'm facing here is on boot. The app is starting, but the layout is not loaded and stales. I was trying to pus some spy messages and even create a file in Activity_Create,
in the first line. The file is not created. I have root access and I can check with adb. If I'm inserting the USB cable after boot and launcher is shown, everything works.
It keep the permissions, everything is fine. But if I'm booting with the connected cable, then it's stalled. Even this simple application below fails. It shows the application title, but not moving forward, loading the activity layout. If I'm pressing back button on navigation bar, the application closes, the boot sequence continues with launcher loading. Disconnecting and reconnecting the USB cable after that, is triggering the app and everything works.

Here is the minimum app I'm testing with:
Application example:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Also, my manifest file si the following:

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.

SetActivityAttribute(Main, android:directBootAware, "true")
SetActivityAttribute(Main, android:launchMode, "singleTask")
CreateResource(xml, device_filter.xml,
    <!-- Your device CH340G for PICOVEND EZ boards -->
    <usb-device vendor-id="6790" product-id="29987"/>

        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />   


And, to be more interesting, checking for active tasks, sometimes (not every time) I found 2 (like in the picture). If I'm selecting one of them, the application runs correctly. Also, when only one is available, if I'm selecting that, it is not working.
If, then, I'm switching to the other one, it is also run correctly.
I'm stuck here for the moment, I though I've made some mistakes in the main app, which is way bigger, that is why I've created the above one, for testing basic behavior, just to find that it's misbehaving in the same way.

Any hint will be highly appreciated, since the app should be able to work in an unattended environment and runtime permissions for UsbManager are not persistent, the only persistence is working by using intent-filter in the manifest, but with the above described issue.


  • tasks001.jpg
    287 KB · Views: 158

Mihai Rainer Jr.

Licensed User
I've changed to "singleTask" because sometimes it was starting twice, thought that the default singleTop may be a problem.
Switched back to default, same behavior. But, there is always a but šŸ˜
I've, somehow, missed the #StartAtBoot attribute. Switched it to true and all is working, by magic :D
I've even tried with another test application, with 2 activities, Main with
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />   


and mn with
AddActivityText(mn, <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />

and is working as supposed:
- on the first run after the fresh install, inserting the USB cable, the app is asking for USB permission (through "Main" because of the intent filter in the manifest);
- set the second activity as the default launcher through Android system;
- reboot with the USB cable connected - first there is a white background screen (Main starting, waiting for boot to finish, showing the layout then closes, because I have instructed to do that in the app);
- Home launcher activity starts and correctly communicating over USB cable, permission retained from the first execution.

Rebooted few times, behavior is correct and consistent.

What can I say? Erel, you are a genius! And I mean it! šŸ„°
Upvote 0