Android Tutorial Android Usb Host Tutorial - AdbTest

Status
Not open for further replies.

3394509365

Active Member
Licensed User
Longtime User
thanks

are these values?
If inter.InterfaceClass = 255 AND inter.InterfaceSubclass = 66 Then

how do I know which ones are the right ones?

do not you think it might be a problem of the platform is not installed correctly?
 

walterf25

Expert
Licensed User
Longtime User
Hi Erel, I followed this example and used it as a base for some projects i'm working on, I wanted to see if you could clarify a few things for me.

I'm not to sure i understand what this functions do exactly, i'am seeing some issues where the data i'm expecting sometimes does not come in in the order they should.
The device sends 64 bytes on every packet, and the first byte is the length of the actual data to be processed or retrieved.
For example if I receive the follwing...
0x01, 0x28, 0x10, 0x53, ..............................
the first byte is 1 so it tells me to grab the first byte right after the first byte, in this case it would be 0x28.

Each readings looks like this
(g5707239706230)5B
The last two numbers are the last two numbers of the checksum value.

I then continue to listen for data and put all the bytes received into an array until i have the amount of bytes that make up one reading.
I do this until i have collected all the readings available from the device, usually 500 readings.

The functions I need to understand are the following....

B4X:
Sub SendOutRequest(Name As String, Data() As Byte, Length As Int)
    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

and the next question is this, Every time i send out a command to the device, does it need to be followed by the SendInRequest("name", 64) command?

I guess i'm still trying to get my head around how this works exactly, I have everything working fine so far, but i get a checksum error from time to time, since i calculate the checksum value and compare it to the checksum value sent along with each reading, if the values do not match then it is a checksum error and i have to restart the download all over again. When I get a checksum error I check the reading that generated the checksum error and it looks like this.

(g57072395B)706230
when it should look like this
(g5707239706230)5B
Notice that the 5B value which is the checksum value is inside the parenthesis and the 706230 is outside the parenthesis. I need to figure out why this happens.

I know you're busy with other important stuff Erel, but i need your help with this.

If it helps this the part of the code where i extract the data coming in.

B4X:
Dim finalstring1 As String
Dim newhex1 As String
Dim len1 As Int
Dim conv1 As ByteConverter
Dim model1() As Byte
hex1 = conv1.HexFromBytes(Request.Buffer)
Dim raf1 As RandomAccessFile
raf1.Initialize3(Request.Buffer, True)
len1 = unsigned(Request.Buffer(0))   'Extract the first byte to know how many bytes to process from this packet
Dim pattern0 As String = "\(([^)]+)\)([0-9A-F][0-9A-F])"  
Dim match01 As Matcher
If len1 < 64 Then
totalstring = totalstring + len1
Dim mDevice(len1) As Byte    'array to place received bytes in
raf1.ReadBytes(mDevice, 0, len1, raf1.CurrentPosition + 1)   'retrieve only the amount of bytes indicated by the first byte len1
newhex1 = conv1.HexFromBytes(mDevice)
model1 = conv1.hextobytes(newhex1)
finalstring1 = BytesToString(model1, 0, model1.Length, "ASCII")   'convert bytes to string.
complete.Append(finalstring1)  'collect string until one reading is complete.

match01 = Regex.Matcher(pattern0, complete)  'checks if a reading is found.

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Hi Erel, and thanks for the reply, the NewData code is this

B4X:
Sub Connection_NewData (Request As UsbRequest, InDirection As Boolean)
If InDirection = True AND Wakecmd = True Then
Dim finalstring1 As String
Dim newhex1 As String
Dim len1 As Int
Dim conv1 As ByteConverter
Dim model1() As Byte
hex1 = conv1.HexFromBytes(Request.Buffer)
Dim raf1 As RandomAccessFile
raf1.Initialize3(Request.Buffer, True)
len1 = unsigned(Request.Buffer(0))   'Extract the first byte to know how many bytes to process from this packet
Dim pattern0 As String = "\(([^)]+)\)([0-9A-F][0-9A-F])" 
Dim match01 As Matcher
If len1 < 64 Then
totalstring = totalstring + len1
Dim mDevice(len1) As Byte    'array to place received bytes in
raf1.ReadBytes(mDevice, 0, len1, raf1.CurrentPosition + 1)   'retrieve only the amount of bytes indicated by the first byte len1
newhex1 = conv1.HexFromBytes(mDevice)
model1 = conv1.hextobytes(newhex1)
finalstring1 = BytesToString(model1, 0, model1.Length, "ASCII")   'convert bytes to string.
complete.Append(finalstring1)  'collect string until one reading is complete.

match01 = Regex.Matcher(pattern0, complete)  'checks if a reading is found.
end if
end if
End Sub

this is just part of it, i'm attaching the service file which takes care of all the incoming data, can you please take a look at it and let me know what i'm doing wrong.

Thanks,
Walter


OutRequests and InRequests are two lists that hold unused UsbRequest objects. The idea is to reuse requests instead of creating new ones. This is an optimization.

Where is your NewData code?
 

Attachments

  • Check_USB.bas
    35.8 KB · Views: 533

walterf25

Expert
Licensed User
Longtime User
Why are you converting the bytes to a string? Do they represent a string?
Hi Erel, yes the response is actually a string, for example the response from a wake Up command is "(^*)D9" and the response from a getmodel command is "(iMAR)9A" etc....

Thanks,
Walter
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Every time i send out a command to the device, does it need to be followed by the SendInRequest("name", 64) command?
You need to send an "in" request whenever you want to fetch data from the from USB device. This is how the USB protocol works. The device cannot sent messages to the host. Only the host can send messages to the device, so everytime you want to receive something you need to first send an in request.
 

walterf25

Expert
Licensed User
Longtime User
Yes i understand that part, the issue i'm seeing is that sometimes if i send a wake up command, i sometimes also receive the reponse from the previous command, it seems like i need to flush the buffer or something, but don't know how to do that on the device.

Thanks,
Walter
 

JTmartins

Active Member
Licensed User
Longtime User
Correct me if I'm wrong, please

Not all phones or tablets will support USB host.

In my case I have here a android 4.1.1 phone & tablet. However nothing happens when I connect a mouse using the OTG cable.

So I've used a little app available in google play called USB Host Check, wich presented me with a couple of red crosses saying that my devices did not have USB host support enaled. The red crosses pointed to

android.hardware.usb.host.xml
handheld_core_hardware.xml
tablet_core_hardware.xml

So I believe that, without rooting, I won't be able to use anything I connect to the USB port in my devices, so it will be a waste of time to try B4A to "talk" with my USB smartcard reader using this 2 devices I have (I do not want to root this ones, at the moment)

Am I correct ?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
USB host is a "non-mainstream" feature in Android. Which means that not all manufacturers implemented this feature.

On many devices you can enable this feature by editing android.hardware.usb.host.xml. However it requires rooting the device.

If the mouse is not detected then your device probably doesn't support USB host mode.
 

ViMeAv ICT

Member
Licensed User
Longtime User
I now use printershare to print directly with OTG cable to usb printer.
I need to print to a thermal transfer printer (ZPL) which is not supported by printershare.
How can I use this lib to send ZPL to a usb printer by OTG cable?
 

ViMeAv ICT

Member
Licensed User
Longtime User
A few years ago I implemented many times the zpl protocol to send this serial or by copy file.txt to lpt1.
But my question is, how can I send this by usb, or is this not possible?

The guys of printershare also have a generic print solution for unknown printers,
I assume otherwise (known printer) they render the pages and send them as image over the usb.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…