hello good day.
if :
Sub btnsharp_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_sharp))
End Sub
then 30x data should be received every 400 ms .
main:
starter:
AsynStreamsText :
greeting
if :
Sub btnsharp_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_sharp))
End Sub
then 30x data should be received every 400 ms .
main:
B4X:
Sub Process_Globals
End Sub
Sub Globals
Dim btnvor As Button
Dim btnzur As Button
Dim btnlinks As Button
Dim btnhlinks As Button
Dim btnmitte As Button
Dim btnhrechts As Button
Dim btnrechts As Button
Dim btnv0 As Button
Dim btnv1 As Button
Dim btnv2 As Button
Dim btnser0 As Button
Dim btnser1 As Button
Dim btnsharp As Button
Dim text_vor As Byte
Dim text_zur As Byte
Dim text_links As Byte
Dim text_hlinks As Byte
Dim text_mitte As Byte
Dim text_hrechts As Byte
Dim text_rechts As Byte
Dim text_v0 As Byte
Dim text_v1 As Byte
Dim text_v2 As Byte
Dim text_ser0 As Byte
Dim text_ser1 As Byte
Dim text_sharp As Byte
Private lblStatus As Label
Private btnConnect As Button
Private lblMessage As Label
Private ProgressBar1 As ProgressBar
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
text_vor=118
text_zur=122
text_links=108
text_hlinks=107
text_mitte=109
text_hrechts=104
text_rechts=114
text_v0=119
text_v1=115
text_v2=120
text_ser0=113
text_ser1=117
text_sharp=102
End Sub
Sub Activity_Resume
SetState
End Sub
Public Sub SetState
btnvor.Enabled = Starter.connected
btnzur.Enabled = Starter.connected
btnlinks.Enabled = Starter.connected
btnhlinks.Enabled = Starter.connected
btnmitte.Enabled = Starter.connected
btnhrechts.Enabled = Starter.connected
btnrechts.Enabled = Starter.connected
btnv0.Enabled = Starter.connected
btnv1.Enabled = Starter.connected
btnv2.Enabled = Starter.connected
btnser0.Enabled = Starter.connected
btnser1.Enabled = Starter.connected
btnsharp.Enabled = Starter.connected
btnConnect.Enabled = Not(Starter.connected)
ProgressBar1.Visible = Starter.connecting
Dim status As String
If Starter.connected Then
status = "connected"
else if Starter.connecting Then
status = "trying to connect..."
Else
status = "disconnected"
End If
lblStatus.Text = $"Status: ${status}"$
End Sub
Public Sub MessageFromDevice(msg As String)
lblMessage.Text = msg
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnvor_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_vor))
End Sub
Sub btnzur_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_zur))
End Sub
Sub btnlinks_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_links))
End Sub
Sub btnhlinks_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_hlinks))
End Sub
Sub btnmitte_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_mitte))
End Sub
Sub btnhrechts_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_hrechts))
End Sub
Sub btnrechts_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_rechts))
End Sub
Sub btnv0_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_v0))
End Sub
Sub btnv1_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_v1))
End Sub
Sub btnv2_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_v2))
End Sub
Sub btnser0_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_ser0))
End Sub
Sub btnser1_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_ser1))
End Sub
Sub btnsharp_Click
CallSub2(Starter, "SendMessage", Array As Byte(0, text_sharp))
End Sub
Sub btnConnect_Click
CallSub(Starter, "Connect")
End Sub
Sub lblStatus_Click
End Sub
starter:
B4X:
Sub Process_Globals
Private serial As Serial
Private admin As BluetoothAdmin
Private ast As AsyncStreamsText
Public connected As Boolean
Public connecting As Boolean
End Sub
Sub Service_Create
serial.Initialize("serial")
admin.Initialize("admin")
End Sub
Public Sub Connect
admin.StartDiscovery
connecting = True
CallSub(Main, "SetState")
End Sub
Private Sub admin_DeviceFound (Name As String, MacAddress As String)
Log($"Device found: ${Name}"$)
If Name = "HC-06" Then
Log("Trying to connect...")
admin.CancelDiscovery
serial.Connect(MacAddress)
End If
End Sub
Private Sub admin_DiscoveryFinished
connecting = False
End Sub
Private Sub Serial_Connected (Success As Boolean)
If Success Then
If ast.IsInitialized Then ast.Close
ast.Initialize(Me, "Ast", serial.InputStream, serial.OutputStream)
Log("Connected")
connected = True
Else
Log(LastException)
End If
connecting = False
CallSub(Main, "SetState")
End Sub
Public Sub SendMessage(msg() As Byte)
ast.astreams.Write(msg) 'sending bytes, not text
End Sub
Private Sub ast_NewText (Text As String)
CallSub2(Main, "MessageFromDevice", Text)
End Sub
Private Sub ast_Terminated
connected = False
CallSub(Main, "SetState")
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
End Sub
AsynStreamsText :
B4X:
#Event: NewText (Text As String)
#Event: Terminated
'version: 1.00
'Class module
Sub Class_Globals
Private mTarget As Object
Private mEventName As String
Public astreams As AsyncStreams
Public charset As String = "UTF8"
Private sb As StringBuilder
End Sub
Public Sub Initialize (TargetModule As Object, EventName As String, In As InputStream, out As OutputStream)
mTarget = TargetModule
mEventName = EventName
astreams.Initialize(In, out, "astreams")
sb.Initialize
End Sub
'Sends the text. Note that this method does not add end of line characters.
Public Sub Write(Text As String)
astreams.Write(Text.GetBytes(charset))
End Sub
Private Sub astreams_NewData (Buffer() As Byte)
Dim newDataStart As Int = sb.Length
sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
Dim s As String = sb.ToString
Dim start As Int = 0
For i = newDataStart To s.Length - 1
Dim c As Char = s.CharAt(i)
If i = 0 And c = Chr(10) Then '\n...
start = 1 'might be a broken end of line character
Continue
End If
If c = Chr(10) Then '\n
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
start = i + 1
Else If c = Chr(13) Then '\r
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
i = i + 1
End If
start = i + 1
End If
Next
If start > 0 Then sb.Remove(0, start)
End Sub
Private Sub astreams_Terminated
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Private Sub astreams_Error
Log("error: " & LastException)
astreams.Close
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Public Sub Close
astreams.Close
End Sub
greeting