Jump to content
Sign in to follow this  

[Android] How to capture characters with a bluetooth device

Recommended Posts


I'm facing a problem that may be more of an Android problem than a "Delphian" one.

My goal:
Capture the barcode read by a bluetooth handheld device in an Android app developed in Delphi FMX (Delphi Rio 10.3.3).

Principle of operation:

  •     The handheld device transmits the barcode as a sequence of characters entered on the keyboard.
  •     The Windows/Android compatible handheld device is configured to provide the code as: 1 character to indicate the beginning of the barcode ("!"), one character to indicate the type of barcode ("c" to indicate that it is an EAN8 barcode), the characters encoded in the barcode (8 characters for an EAN8) and the CR LF characters to indicate the end of the barcode.
  •     The app retrieves each character received on an OnKeyDown event and forms the barcode by harvesting the characters one by one (reason for the character that indicates the beginning of the barcode and an end character of the barcode).


Problem encountered:

Under Windows and Android, we do not get the same result.


If we read a barcode of type EAN8 (=8 digits):


Under Windows, we get: !C20010003 CR LF (this corresponds to the configuration of the showerhead)


Under Android, nothing works anymore:


1. CR and LF are not detected (I seem to remember that Unix does not process CR and other LF)


2. If we visualize the characters captured in a Tmemo by displaying the value of the string that has captured one by one the characters received, we have !C20010003 (without CR and LF)


3. I control the number of characters received by checking length(string)=21 (21 and not 10 which would be the length of a string "!C20010003"!!)


4. So I check the ASCII code of the characters received at the onKeyDown event:


33 99 0 50 0 48 0 48 0 49 0 48 0 48 0 48 0 51 0 0 0


Which corresponds to:


! C 2 0 0 1 0 0 0 3 0

We have the correct ASCII code followed by a 0 after each character and a final 0 instead of CR LF.



Under Android, the event receives 21 characters, the string shows 10 characters but indicates a length of 21 characters and adds at the end a "0" which corresponds to "I don't know what".


Under windows, the event receives 12 characters (the 10 visible characters + CR + LF), the length of the string=12 and the string shows the 10 visible characters.

Does anyone have any experience in handling strings in Android, an explanation of how it works under Android and a solution to detect CR and LF sent under Android?


Thanks for your help

Share this post

Link to post

Which "bluetooth handheld device"?


Yes! A wireless barcode reader connected to a smartphone via bluetooth!


I think that the last characters received could be ASCII=0 followed by a zero. ASCII=0 corresponds to NUL. The string sent with a CR LF could be treated as a string with terminal zero

Share this post

Link to post

I think the response is workable for you, no matter where the problem comes from.

As long as you see double-zero at the end, you know its the "0-version", otherwise the normal version.


The only question is maybe, does all Android devices behave the same, or is this caused by a special driver of. e.g. LG devices ?


Have you checked with other Phones too, and/or with other BT keyboards ?

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this