Android Question Capturing USB_DEVICE_ATTACHED and USB_DEVICE_DETACHED event

Filippo

Expert
Licensed User
Longtime User
Hi,

up to android version 10, my intent filter in Manifest has worked very well, ie the response(USB permission request) was fast(about 1-2 seconds).
Now with android 12 is the response(USB permission request) very slow(about 20 seconds) and the event USB_DEVICE_ATTACHED is not recognized.

Is there anything additional that needs to be considered for android 12?

B4X:
'intent-filter
AddReceiverText(smcmm, <intent-filter>
 <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
 <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
</intent-filter>)

usbService:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.Action="android.hardware.usb.action.USB_DEVICE_ATTACHED" Then
        If Not(IsPaused(Starter)) And Not(IsPaused(Main)) Then
            'Log("USB_DEVICE_ATTACHED:" & StartingIntent.ExtrasToString)
            '...
        End If
    Else If StartingIntent.Action="android.hardware.usb.action.USB_DEVICE_DETACHED" Then
        If Not(IsPaused(Starter)) Then
            'Log("USB_DEVICE_DETACHED:" & StartingIntent.ExtrasToString)
            '...
        End If
    End If
    Service_Stop
End Sub

Sub Service_Destroy

End Sub

Public Sub Service_Stop
    StopService(Me)
End Sub
 
Last edited:
Solution
I have now solved my problem with a "broadcast".
Now everything works without any problem, even if several apps are waiting for this event.

Just replace the service with this class.

B4X:
Sub Class_Globals
    Private broadcast As BroadCastReceiver
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    broadcast.Initialize("broadcast")
    broadcast.registerReceiver("android.hardware.usb.action.USB_DEVICE_ATTACHED")
    broadcast.registerReceiver("android.hardware.usb.action.USB_DEVICE_DETACHED")
    
    'Set priority to maximum value
    broadcast.SetPriority(2147483647)
End Sub

Private Sub broadcast_OnReceive(Action As String, Extras As Object)
    'Execute so that other...

Filippo

Expert
Licensed User
Longtime User
Check the unfiltered logs. Do you see any error message after you attach the usb?

In the unfiltered-logs I noticed this line of text:
 

Attachments

  • unfiltered-logs.txt
    148.3 KB · Views: 55
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Check the unfiltered logs. Do you see any error message after you attach the usb?
At the first test my OTG adapter was somehow defective.
Now it works, but the response is much too slow.

unfiltered-logs:
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I don't see anything in the logs from the last post. The foreground error can be a real issue as the OS prevents services from starting in the background.
When I connect the USB device, the app is in the foreground. The service does not need to be started in background.

Here is another test. Now has something to do with the time(ForegroundServiceDidNotStartInTimeException):
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{c09bed7 u0

This happens because you disabled the automatic foreground mode. You need to call Service.StartForeground yourself, in Service_Start.
Hi Erel,

I have now made some progress.

Whether the "Service.AutomaticForegroundMode" function is enabled or not makes no difference.
I think the problem is that I have 4 apps responding to the same USB device.
Connecting the USB device is registered by the current app after a few seconds, only removing it takes about 3 minutes for the app to give the info.

If I use the function "Service.StartForeground(1, CreateNotification("..."))" then I don't see any difference.
B4X:
Private Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("USB-Trigger", Body, Main)
    Return notification
End Sub

 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
When I uninstall all other applications, everything works as it should.
That's very nice, but that can't be the solution, can it?
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
This log output is from a cell phone with android 10.
Here it works without problems even if the other apps are installed.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
Hi Erel,
thank you very much for your answer.
But I don't understand one thing, if all other apps are not running (all processes are finished), why should they get the event that a usb-device is connected?
Normally only the app that is currently running should receive this event.
Ok, if this is a system error, then nothing can be done.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But I don't understand one thing, if all other apps are not running (all processes are finished), why should they get the event that a usb-device is connected?
Normally only the app that is currently running should receive this event.
Intent filters do not depend on the app running or not. The system starts the app when it receives the intent.
 
Upvote 0

Filippo

Expert
Licensed User
Longtime User
I have now solved my problem with a "broadcast".
Now everything works without any problem, even if several apps are waiting for this event.

Just replace the service with this class.

B4X:
Sub Class_Globals
    Private broadcast As BroadCastReceiver
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    broadcast.Initialize("broadcast")
    broadcast.registerReceiver("android.hardware.usb.action.USB_DEVICE_ATTACHED")
    broadcast.registerReceiver("android.hardware.usb.action.USB_DEVICE_DETACHED")
    
    'Set priority to maximum value
    broadcast.SetPriority(2147483647)
End Sub

Private Sub broadcast_OnReceive(Action As String, Extras As Object)
    'Execute so that other applications do not receive this event
    broadcast.AbortBroadcast
    
    If Action="android.hardware.usb.action.USB_DEVICE_ATTACHED" Then
        'your code
    Else If Action="android.hardware.usb.action.USB_DEVICE_DETACHED" Then
        'your code
    End If
End Sub
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…