Android Question Bluetooth connection

gezueb

Active Member
Licensed User
Longtime User
Hi,
I have a problem with bluetooth connection. I have an Arduino that send some data over a bluetooth connection. I use code derived from your bluetooth example. The connection is established ok, but the NewText event in chatactivity is never raised although the arduino sends (ASCII)data. I use the data transfer without prefix. There are no compiler errors or java errors.
Here are the main parts of the code:

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("2")
If AStream.IsInitializm_ed = False Then AStream.Initialize("CoDisplay", "AStream", Main.serial1.InputStream, Main.serial1.OutputStream)
End If
txtLog.Text = "" 'clear Text
txtLog.Width = 100%x
End Sub

Sub AStream_NewText (InText As String)
' for unknown reasons, its newtext and not NewText in the above call. Problem must be in the class AsyncStreamsText
LogMessage(InText)
' Parse the temperature from the entire string
If InText.Length > 5 Then
If InText.SubString2(0,6) = "Ext = " Then
Temp = InText.SubString2(6,InText.Length)
Temperature.Text = Temp
End If
End If
ToastMessageShow("Hallo", True)


End Sub

Thanks for any help or suggestions,
Georg
 

gezueb

Active Member
Licensed User
Longtime User
I have found one problem: I used a baud rate of 115k which seems to be too much for the B4A side. I made the obviously wrong assumption that Bluetooth dos not care about the baudrate. I reduced it to 9600 bit/s and now I get an event, but alas, also a java runtime error that the astream_newtext sub was not recognized. I tried to change the call in the chat_activity from AStreams_NewText to all lowercase, however that was no cure. I now that Java is much more case-sensitive than Basic, but I am unable to find the fault. I am as always grateful for a hint,
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Declaration of AStream (or astream ...) is like this:
/Sub Process_Globals
Dim astream As AsyncStreamsText
End Sub/

Anyway, I have not found a bug but a workaround for the problem:
When I change the call in AsyncStreamText from

CallSubDelayed2(mTarget, mEventName & "_newtext", s.SubString2(start, i)) 'gives java run time error
to
CallSubDelayed2(mTarget, "astream_newtext", s.SubString2(start, i))

e.g. call the event with an explicit string as event name rather than let it concatenate by the call, then the event is raised properly. This is kind of weird and may be a bug in the interface between basic an java.
Anyway, I am most thankful for your advice.
Georg
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Here we go. The code is by no means finished, it should finally be an artificial horizon feed with data from an arduino with a 10DOF chip. The code is close to the example, I just changed all references to AStreams anywhere to lower case, but as mentioned before, this did not help. Have a look at the mod I made to AsyncStreamText. It works like this. To reproduce the problem, uncomment the two subdelayed calls and transmit anything into the app via bluetooth.
Thanks for your support, Georg
 

Attachments

  • AHRS Arduino.zip
    430.6 KB · Views: 139
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change:
B4X:
astream.Initialize("Com_Display", "astream_", Main.serial1.InputStream, Main.serial1.OutputStream)
To:
B4X:
astream.Initialize("Com_Display", "astream", Main.serial1.InputStream, Main.serial1.OutputStream)
Or better:
B4X:
astream.Initialize(Me, "astream", Main.serial1.InputStream, Main.serial1.OutputStream)
The underscore is added automatically.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…