Route keyboard out via astreams

briant1972

Member
Licensed User
Longtime User
I'm trying to send keyboard strokes out via bluetooth using astreams. The majority of my application works just fine sending data out, but part of the device I'm interfacing with functions like a good old fashioned terminal and it can't handle when a line gets sent all at once, like when you enter text into a edittext and send it when enter is pressed. I want to send each keystroke as it occurs. Any suggestions???
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You have several options. You can send the complete line one character after another.
You can use EditText_TextChanged event and send the last character each time. This might be problematic if the user changes the cursor position.
You can also remove the EditText and handle Activity_KeyPress to both display the characters and send them to the other device.
 
Upvote 0

briant1972

Member
Licensed User
Longtime User
I tried to send one character at a time by using the textchanged event, but I ran into issues with the last character getting sent again after a carriage return or a space for some reason. I'd type something like run visutl and runn visutll would get sent. After trying to figure out what that was happening, I decided to just use the enterpressed event, but I had to send the bytes one at a time rather than just take the whole string, convert it to bytes a send it all. This seemed to slow things down enough to allow the host system to handle the characters one at a time. There still seems to be something funky with the carriage return at the end where the host seems to handle it for some commands just fine but not for others. I'm going to keep plugging away at this but if you see any obvious problems, please let me know.

Thanks for the help.

Sub EditText1_EnterPressed
If AStreams.IsInitialized = False Then Return
Dim I As Int
Dim IndText As String
Dim IndByte() As Byte
Dim BufferLength As Int
Dim buffer() As Byte

buffer = EditText1.Text.GetBytes("UTF8")
BufferLength = buffer.Length
'ToastMessageShow(BufferLength, False)
For I = 1 To EditText1.Text.Length
IndText = EditText1.Text.SubString2(I-1,I)
IndByte = IndText.GetBytes("UTF8")
Astreams.Write(IndByte)
'ToastMessageShow(IndText,False)
Next
'Astreams.Write(buffer)
Dim LineFeed () As Byte
LineFeed = Enter.GetBytes("UTF8")
EditText1.Text = ""
AStreams.Write(LineFeed)


End Sub
 
Upvote 0

briant1972

Member
Licensed User
Longtime User
I added some code to display the hex values being sent, and it seems that CRLF is being sent as 0A hex, which is "line feed". I made some changes so 0D, which is "carriage return" is sent when enter is pressed. Those old ASCII tables still come in handy sometimes:). Things are working fine now. I'm really close to the end of this project now. It's an operator keyboard for an old process control system that is replacing a combination of membrane buttons, an infrared matrix (an old type of "touch screen"), CRT like display that the infrared matrix sits over (functioning as "buttons" with dynamic labels), and something similar to a WYSE terminal. I'll send screenshots when I'm done. The reason I started this project is the 20 year old operator keyboards are unreliable and are in the range of $12,000 to replace, if you can find one.
 
Upvote 0
Top