Usb

KiloOne

Member
Licensed User
Longtime User
I have purchased and installed enterprise.
I have set paths to javac.exe and android.jar (v12).
I have modified tabhost example (see code below) to start building a program that finds my USB device (vendorID 1240 productID 63).
From what I gather from 11289-android-usb-host-tutorial-adbtest.html#post63117, I should be able to call FindAdbDevice from Sub btnNext1_Click.

Problem is that I get a compile error :

Error description: Unknown type: usbrequest
Are you missing a library reference?
Occurred on line: 106
Sub Connection_NewData (Request As UsbRequest, InDirection As Boolean)

I thought my library problems would disappear when I bought and installed enterprise, what am I missing for compiling?

I know there are other errors in my code but I figure I can debug them if it compiles.

Thanks
Dale

B4X:
#Region Module Attributes
   #FullScreen: True
   #IncludeTitle: False
   #ApplicationLabel: TabHost
   #VersionCode: 1
   #VersionName: 
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: True
#End Region

Sub Process_Globals
   
End Sub

Sub Globals
   Dim TabHost1 As TabHost
   Dim txtName, txtAnimal, txtColor As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   Dim bmp1, bmp2 As Bitmap
   bmp1 = LoadBitmap(File.DirAssets, "ic.png")
   bmp2 = LoadBitmap(File.DirAssets, "ic_selected.png")
   
   TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "page1") 'load the layout file of each page
   TabHost1.AddTab("Color", "page2") 
   TabHost1.AddTab("Animal", "page3")
End Sub
Sub Activity_Pause (Finishing As Boolean)
   
End Sub
Sub Activity_Resume

End Sub
Sub btnNext1_Click
   FindAdbDevice 
   'TabHost1.CurrentTab = 1 'move to next tab
End Sub

Sub btnNext2_Click
   TabHost1.CurrentTab = 2 'move to next tab
End Sub

Sub btnDone_Click
   Dim sb As StringBuilder
   sb.Initialize
   sb.Append("You have entered:").Append(CRLF)
   sb.Append("Name: ").Append(txtName.Text).Append(CRLF)
   sb.Append("Color: ").Append(txtColor.Text).Append(CRLF)
   sb.Append("Animal: ").Append(txtAnimal.Text)
   Msgbox(sb.ToString, "")
End Sub

Sub TabHost1_TabChanged
   Activity.Title = "Current Tab = " & TabHost1.CurrentTab 
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.vendorid = 1240 AND inter.productid = 63 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_BULK 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.")

   If manager.HasPermission(device) = False Then manager.RequestPermission(device)
         
    connection = manager.OpenDevice(device, interface, True)
'Log("Starting connection")
'Dim data(), msg() As Byte
'data = ConvertStringToBytesWith0("host::" & Chr(0))
'msg = CreateMessage(A_CNXN, 0x01000000, 4096, data)
'SendOutRequest("Msg", msg, 24)
'SendOutRequest("Data", data, data.Length)
'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 sendData As Boolean
    If Request.Name = MSG_READ Then
        Dim raf As RandomAccessFile
        raf.Initialize3(Request.Buffer, True)
        Dim command As Int
        command = raf.ReadInt(0)
        If command = A_CNXN OR command = A_WRTE Then
            lastCommand = command
            SendInRequest(DATA_READ, raf.ReadInt(12)) 'read the data request
            sendData = True
        Else
            DispatchMessage(command, raf.ReadInt(4), "")
        End If
    Else If Request.Name = DATA_READ Then
        Dim s As String
        s = BytesToString(Request.Buffer, 0, Request.Buffer.Length, "UTF8")
        DispatchMessage(lastCommand,0, s)
    End If
    If sendData = False AND connection.IsInitialized Then
        SendInRequest(MSG_READ, 24)
    End If
    ReleaseRequest(Request, InRequests)
    connection.ContinueListening
End Sub
 

stevel05

Expert
Licensed User
Longtime User
Have you selected the library it in the Libs tab (bottom right of the ide)?
 
Upvote 0

KiloOne

Member
Licensed User
Longtime User
Compiling fine now that I got permission to download USBexample.zip and I got the rest of the necessary code added.

BUT, it is not immediately obvious how to enumerate for vendorID and productID versus the example usage of InterfaceClass and InterfaceSubClass (no inter. member that relates to productID and VendorID).

Anyone with the quick knowledge on this?

Thanks,
Dale

Found this in the defined usbdevice (ud).
 
Last edited:
Upvote 0

KiloOne

Member
Licensed User
Longtime User
Wow! That was quick, USB working already.

I think I am really going to like this IDE!!!!:)

I guess too many years working with VB6.

No more struggling to learn much Java and Eclipse etc.

Thanks for all the effort to make basic4android!!!:sign0098:
 
Upvote 0
Top