Android Question USB HID problem

jefflynn1974

Member
Licensed User
Longtime User
Hi,
As the title says, I have a problem connecting a HID device to an Android phone. The device I want to communicate with is an ARM based board that I created some years ago and have been using with a PC program and now I want an Android app. I'm using the ADB test project as a base and started modifying it. I changed the InterfaceClass and InterfaceSubClass and it seems to be working with one exception: when I read the data that the device sends back I get only zeros. The boards receives the command from the phone and replies to it, I was able to check that using an ARM debugger. What am I doing wrong?

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: USB
    #VersionCode: 2
    #VersionName: 1.01
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim manager As UsbManager
    Dim connection As UsbDeviceConnection
    Dim outEndpoint, inEndpoint As UsbEndpoint
    Dim device As UsbDevice
    Dim interface As UsbInterface
    Dim MSG_READ, DATA_READ As String
    MSG_READ = "Msg-Read" : DATA_READ = "Data-Read"
    Dim InRequests, OutRequests As List
End Sub

Sub Globals

    Dim btnConnect As Button
    Dim btnDisconnect As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        manager.Initialize
        InRequests.Initialize
        OutRequests.Initialize
    End If
    Activity.LoadLayout("1")
End Sub

Sub Activity_Pause(UserClosed As Boolean)
   
End Sub

Sub btnDisconnect_Click
    If connection.IsInitialized Then
        connection.StopListening
        OutRequests.Clear
        InRequests.Clear
        Log("Stopped")
    End If
End Sub

Sub btnConnect_Click
    If connection.IsInitialized = False Then
        FindAdbDevice
        CheckPermission
    End If
End Sub

Sub CheckPermission
    If device.IsInitialized Then 'It will only be initialized if a device was found
        If manager.HasPermission(device) = False Then
            manager.RequestPermission(device)
        Else
            'There is permission. Connect if neccessary.
            If connection.IsInitialized = False Then
                MakeConnection
            End If
        End If
    End If
End Sub

Sub Activity_Resume

End Sub

Sub FindAdbDevice As Boolean
    Dim usbdevices() As UsbDevice
    usbdevices = manager.GetDevices
   
    'Iterate over devices and find the correct one
    For i = 0 To usbdevices.Length - 1
        Dim ud As UsbDevice
        ud = usbdevices(i)
        Log(ud)
        'Iterate over interfaces
        For a = 0 To ud.InterfaceCount - 1
            Dim inter As UsbInterface
            inter = ud.GetInterface(a)
            If inter.InterfaceClass = 3 And inter.InterfaceSubclass = 0 Then
                'found our device and interface
                device = ud
                interface = inter
                'Find correct endpoints
                For b = 0 To interface.EndpointCount - 1
                    Dim endpoint As UsbEndpoint

                    endpoint = interface.GetEndpoint(b)

                    If endpoint.Type = manager.USB_ENDPOINT_XFER_INT Then
                        If endpoint.Direction = manager.USB_DIR_IN Then
                            inEndpoint = endpoint
                        Else If endpoint.Direction = manager.USB_DIR_OUT Then
                            outEndpoint = endpoint
                        End If
                    End If
                Next
            End If
        Next
    Next
    If device.IsInitialized = False Then Log("ADB device not found.")
End Sub

Sub MakeConnection
    Dim data(64), msg(24) As Byte

    connection = manager.OpenDevice(device, interface, True)
    Log("Starting connection")
   
    msg = ConvertStringToBytesWith0("#VERSION")
   
    SendOutRequest("Msg", msg)
    SendInRequest(MSG_READ, 24)
    connection.StartListening("connection")
End Sub

Sub Connection_NewData (Request As UsbRequest, InDirection As Boolean)
    If connection.IsInitialized = False Then Return 'Might happen after we close the connection

    If InDirection = False Then
        ReleaseRequest(Request, OutRequests)
        connection.ContinueListening
        Return 'don't handle OUT requests
    End If

    Dim raf As RandomAccessFile
    Dim s As String

    raf.Initialize3(Request.Buffer, True)
   
    s=""
    For i=0 To 23
        s=s & raf.ReadUnsignedByte(i)
    Next
    Log(s)

    If Request.Name = MSG_READ Then       
    Else If Request.Name = DATA_READ Then
    End If

    ReleaseRequest(Request, InRequests)
    connection.ContinueListening
End Sub

Sub ConvertStringToBytesWith0 (S As String) As Byte()
    s = s & Chr(0)
    Return s.GetBytes("UTF8")
End Sub

Sub SendOutRequest(Name As String, Data() As Byte)
    Dim request As UsbRequest
    request = GetRequest(True)
    request.Name = Name
    request.Queue(Data, Data.Length)
End Sub

Sub SendInRequest(Name As String, Length As Int)
    Dim request As UsbRequest
    request = GetRequest(False)
    request.Name = Name
    Dim Data(Length) As Byte
    request.Queue(Data, Data.Length)
End Sub

Sub GetRequest (Out As Boolean) As UsbRequest
    Dim r As UsbRequest
    Dim RequestList As List
    If Out Then RequestList = OutRequests Else RequestList = InRequests
    If RequestList.Size = 0 Then
        If Out Then
            r.Initialize(connection, outEndpoint)
        Else
            r.Initialize(connection, inEndpoint)
        End If
    Else
        r = RequestList.Get(0)
        RequestList.RemoveAt(0)
    End If
    Return r
End Sub

Sub ReleaseRequest(Request As UsbRequest, RequestList As List)
    RequestList.Add(Request)
End Sub
 

jefflynn1974

Member
Licensed User
Longtime User
Yes, I can. There is not much to monitor though, the device sends back a simple string as a reply to "#VERSION" command, something like "Version:NuCan V2.0...".
 
Upvote 0

divinglog

Member
Licensed User
Longtime User
Were you able to resolve it? I'm also trying to communicate with a HID device and it looks like sending works, but I never receive a response.
 
Upvote 0
Top