Send single character with button_click

imakebeer

New Member
Licensed User
Longtime User
I have been trying to get the bluetooth serial demo to transmit a single predefined character when I press a button. This is my first delve into B4A. Thanks.

:sign0104:
 

kickaha

Well-Known Member
Licensed User
Longtime User
Well if you let everyone know what you have done so far, and what your problems are then maybe we can help.
 
Upvote 0

imakebeer

New Member
Licensed User
Longtime User
I build a circuit with an accelerometer and want to display the data on a tablet screen using Bluetooth. Using the Serial example the data comes across. However, there needs to be some handshaking between the tablet and the circuit micro so it knows when the Bluetooth is connected and can start sending data. So far I have figured out how to remove the "ME" and "YOU". I also added a button that I would like to use to send a single hard-coded character from the tablet to the micro instead of using the soft keyboard.

Thanks :sign0104:

B4X:
'Activity module
Sub Process_Globals
   Dim AStream As AsyncStreams
End Sub

Sub Globals
   Dim txtInput As EditText
   Dim txtLog As EditText
   Dim btnSend As Button
   Dim btnOK   As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
     'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
     AStream.Initialize(Main.serial1.InputStream,Main.serial1.OutputStream, "astream")
   End If
   txtLog.Width = 100%x
End Sub

Sub AStream_NewData (Buffer() As Byte)
  LogMessage(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
   'LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub AStream_Error
   ToastMessageShow("Connection is broken.", True)
   btnSend.Enabled = False
   txtInput.Enabled = False
End Sub

Sub AStream_Terminated
   AStream_Error
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
      AStream.Close
   End If
End Sub

Sub txtInput_EnterPressed
   If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   'LogMessage(txtInput.Text)   
End Sub
Sub btnOK_click

  ' SEND SINGLE CHARACTER HERE

  AStream.Write( ?????("UTF8"))
End Sub
Sub LogMessage(Msg As String)
   txtLog.Text = Msg & CRLF
End Sub
 
Upvote 0

imakebeer

New Member
Licensed User
Longtime User
Thank you Arel.

After a lot of searching I came up with
B4X:
Sub btnOK_click
  Dim buffer(1) As Byte
  buffer(0) = 32
  AStream.Write(buffer)
End Sub

Your solution is much better.
 
Upvote 0
Top