B4J Question USB Devices

Kevin Hartin

Active Member
Licensed User
I am using the USB ADBhost code at https://www.b4x.com/android/forum/threads/android-usb-host-tutorial-adbtest.11289/ as the basis of a SmartCard reader.

In B4A it is working great to find the USB device and I am able to select the correct device based on Class and Subclass. I can send APDU commands, but I am stuck getting any response.

So.. I thought I'd port it to B4J and use a Smartcard logging app to see if I could sniff out the issue.
B4X:
Sub FindSmartCardDevice 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("USBdevices: " & ud)
        'Iterate over interfaces
        For a = 0 To ud.InterfaceCount - 1
            Dim inter As UsbInterface
            inter = ud.GetInterface(a)
            Log("Class|SubClass: " & inter.InterfaceClass & "|" & inter.InterfaceSubclass)
            If inter.InterfaceClass = 11 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)
                    Log("EndPoint Address: " & endpoint.Address & "|Attributes: " & endpoint.Attributes & "|Direction: " & endpoint.Direction & "|EndpointNumber: " & endpoint.EndpointNumber & "|Interval: " & endpoint.Interval & "|MaxPacketSize: " & endpoint.MaxPacketSize & "|Type: " & endpoint.Type)
                    If endpoint.Type = manager.USB_ENDPOINT_XFER_BULK Then
'                        Log("Endpoint  Address: " & endpoint.Address & "|Type: " & endpoint.Type & "|Direction: " & endpoint.Direction)
                        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("Smartcard device not found.")
End Sub
The code seems to go over fine, but on compiling it gives the following error for the line "inter = ud.GetInterface(a)"
Java Error:
Compiling generated Java code.    Error
B4J line: 112
inter = ud.GetInterface(a)
src\b4j\example\main.java:211: error: package android.hardware.usb does not exist
_inter = (anywheresoftware.b4a.objects.usb.UsbManagerWrapper.UsbInterfaceWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.usb.UsbManagerWrapper.UsbInterfaceWrapper(), (android.hardware.usb.UsbInterface)(_ud.GetInterface(_a)));
                                                                                                                                                                                                                                            ^
1 error
Why is there a mention of android in a B4J error? Does the USB library work in B4J?

The Library Manager seems to indicate it is OK
1618882305199.png


Thanks,
Kev
 
Top