Android Question barcode scanner into EditText - CRLF not detected

cwt

Active Member
Licensed User
Longtime User
I am trying to use a barcode scanner with the suffix set to CRLF - the Enter keystoke is appended to the end of the scanned barcode data.

I want to detect the Chr(10) so I can call another sub to use the data scanned into the EditText control.

Below is my code:

text changed:
Sub Part_Number_TextChanged (Old As String, New As String)
    
    Dim ThisPart As String = New
    
    If ThisPart.EndsWith( Chr(10) ) = True Then
        Log("suffix found")
        Dim StringLen As Int = ThisPart.IndexOf( Chr(10) )
        Part_Number.Text = ThisPart.SubString2(0,StringLen).Trim
        AddItem
    End If
    
End Sub

There is nothing that I have been able to do to detect the Chr(10) sent by the barcode scanner - this leads me to think that maybe non-viewable ascii characters are not sent to the TextChanged sub.

I have verified that the barcode scanner is adding the Enter key to the scanned data.

I can program the scanner to send viewable characters as the suffix and this works fine but Enter key and Tab keystrokes are not detected in the TextChanged sub. Am I doing something wrong here?
 

drgottjr

Expert
Licensed User
Longtime User
just to clarify, CRLF means different things to different people. for my barcode scanner, CRLF is chr(13) + chr(10). for B4X, CRLF is chr(10).
if i want my barcode scanner to append chr(10) only, i can tell it to do that. i can also tell it not to append any characters. the so-called ENTER key is chr(13).
perhaps your scanner is appending chr(13)... this might account for why you do not find chr(10) at the end.
 
Upvote 0

cwt

Active Member
Licensed User
Longtime User
My barcode scanner is appending Chr(13) and Chr(10) to the data - a normal Enter keystroke. I can detect neither in the TextChanged sub. If I set the scanner to append a tab keystroke - Chr(9) - this single character cannot be detected either. I can use my own suffix, such as "####" and it works fine. The problem is detecting non-viewable ascii characters.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
it doesn't work in textchanged. if you look at the hex values of the result after edittext has been populated, you'll see either 0D or 0A. at least, i do.
 
Upvote 0

cwt

Active Member
Licensed User
Longtime User
I do not know how to see the hex values of the edit text - how do you do this? And, you confirmed that the non-viewable ascii characters are not sent to the TextChanged sub?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
those characters are, by definition, non-printing characters. they serve other purposes,
but they are certainly part of the byte stream. and you don't need me to tell you what
you already see: textchanged may or may not behave as you expect.

the os interprets these low ascii values as control characters. chr(10) might involve
moving a cursor to a new line (of text). it might involve unsetting the view's focus. it
might be part of a stream of bytes, which when placed in the context of "lines of text"
causes a linefeed in between each "line". but, in such a context, the lines would not
include the chr(10). in common usage, a string of text doesn't end with chr(10).
end of input, however, may be signalled by chr(10) which is not going to be included
in the string. and a string of bytes can terminate with chr(10) (but if you look at those
bytes in a text context, the chr(10) will be dealt with as the os determines in that
context.

B4X:
dim bytes() as byte = barcode.getbytes("ISO8859-1")
gets you your bytes.

agraham's byteconverter library will allow you to print those values as a hex string:
B4X:
log(bc.StringFromBytes(bytes, "ISO8859-1"))

technically, UTF8 should work, but things depend on the symbology your scanner is looking at.

by the way, once you have determined that the barcode actually ends with chr(10), then you just drop it. you don't have to go through all the stuff you had:
dim fixedbarcode as string = originalbarcode.substring2(0,originalbarcode.length - 1)

also if define a string as "12345" & crlf, its length is 6, even though you can't see the crlf. but you can remove it. depending on the contect in which you place this string, it may have already been seen and dealt with by the os.
 
Last edited:
Upvote 0

hung

Active Member
Licensed User
Longtime User
May be you can use a <textarea> field to receive the scan data with crlf, or try multiple lines edittext field.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…