Android Question UsbRequest - "Error Queuing Request"

Dane du Plooy

Member
Licensed User
We have a setup where we are downloading a log from a device over USB. The device sends a packet and then waits for an acknowledgement from the Android side before sending the next packet. In B4A I am thus sending the ack response message after every received packet. On one of my Android devices it works fine, but on another I get "Error Queuing Request" when trying to call UsbRequest.Queue. Interestingly, if I run in Debug mode, I don't get the error - presumably because it is running much slower. Any ideas on how to debug / mitigate / fix this? Our device times out after 100ms if no ack message is received and carries on with the next packet, so things still work, however it is obviously very slow.

The stack when the error occurs is simply:
at anywheresoftware.b4a.objects.usb.UsbDeviceConnectionWrapper$UsbRequestWrapper.Queue(UsbDeviceConnectionWrapper.java:207)

Thank-you for any input / advice!
 

Dane du Plooy

Member
Licensed User
Hi Erel

Thank-you for the excellent support as always.

The full error message is:

*** Service (usblogdownloadservice) Create ***
** Service (usblogdownloadservice) Start **
** Activity (usbdevice) Pause, UserClosed = false **
** Activity (usblogdownload) Create, isFirst = true **
** Activity (usblogdownload) Resume **

usbservice_usbwrite (java line: 687)
java.lang.RuntimeException: Error queuing request
at anywheresoftware.b4a.objects.usb.UsbDeviceConnectionWrapper$UsbRequestWrapper.Queue(UsbDeviceConnectionWrapper.java:207)
at strata360.bluetooth.usbservice._usbwrite(usbservice.java:687)
at strata360.bluetooth.usbservice._write(usbservice.java:698)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1083)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1038)
at strata360.bluetooth.usblogdownloadservice._usbwritemsg(usblogdownloadservice.java:932)
at strata360.bluetooth.usblogdownloadservice._usback(usblogdownloadservice.java:650)
at strata360.bluetooth.usblogdownloadservice._usbdatareceived(usblogdownloadservice.java:895)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1179)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)


The code is:

B4X:
private Sub usbWrite(Data() As Byte) As Boolean
    If Not(mblnConnected) Then Return False
    Dim ur As UsbRequest
    ur.Initialize(musbDevCon, musbEpOut)
    'request.Name = "Msg-Write"
    'Try
        ur.Queue(Data, Data.Length)
        mintWriteQueueSize = mintWriteQueueSize + 1
        'Log("USB queue size " & mintWriteQueueSize)
        Return True
    'Catch
    '    Log(LastException)
    '    usbCheckConnection
    '    Return False
    'End Try
End Sub

public Sub Write(Data() As Byte) As Boolean
    Return usbWrite(Data)
End Sub

Private Sub musbDevCon_NewData(Request As UsbRequest, InDirection As Boolean)
    If musbDevCon = Null Then Return 'Might happen after connection is closed
    If musbDevCon.IsInitialized = False Then Return 'Might happen after connection is closed
    mlngLastActivity = DateTime.Now
    If InDirection = False Then
        musbDevCon.ContinueListening
        mintWriteQueueSize = mintWriteQueueSize - 1
        If mintWriteQueueSize < 0 Then mintWriteQueueSize = 0
        mlngLastWriteQueueDecrement = DateTime.Now
        'Log("USB queue size " & mintWriteQueueSize)
        Return 'Don't handle OUT requests
    End If
    RaiseEventDelayed2("UsbDataReceived", Request.Buffer)
    musbDevCon.ContinueListening
    If Not(usbListen) Then 'Listen for a new incoming packet
        usbCheckConnection
    End If
End Sub

I commented out the Try .. Catch in usbWrite() so that the error would come through via the Bridge Logging in release build. If more of the code is needed then please let me know :).

Thank you!
 
Upvote 0

Dane du Plooy

Member
Licensed User
I think the reason it "works" in Debug mode is that everything runs a lot slower in Debug mode (in effect introducing the "Sleep" you have suggested). I'll try the sleep, I was just wondering if there might be a better solution / more insight into what exactly is happening in the lower levels :).
 
Upvote 0

Dane du Plooy

Member
Licensed User
My apologies for posting to an old thread, but I am facing this problem again, this time using a different embedded compiler (STM32CubeIDE) - this time it seems like no amount of "sleep"-ing will solve anything, and it is simply the number of requests sent which triggers the problem (regardless of the delay between them). Everything works fine when using USB via the PC. This does seem to be some low-level Android issue, as even closing and restarting the app written in B4A does not clear the issue, only unplugging and replugging the USB device.
Any ideas on where to even start trying to debug/fix this? :)
 
Upvote 0

Dane du Plooy

Member
Licensed User
I got a new Android device today to test with (of the 4 I had here only one would even attempt to work as a USB-HID host) and the new device doesn't have the issue. Seems like USB Host on Android is really temperamental (?).
 
Upvote 0
Top