lang="b4x" title="Byte array variable"]
'arry1: 75, 28, 82, 158, 127, 0, 1, 29
'array2: 83, 68, 82, 95, 127, 0, 0, 222
'arry3: 11, 22, 87, 127, 158, 0, 1, 29
'arry4: 59, 32, 82, 23, 95, 0, 65, 222
'array5: 57, 68, 54, 127, 127, 0, 0, 254
Sub Timer1_Tick
If connected Then
Dim a() As Byte = Array As Byte(75, 28, 82, 158, 127, 0, 1, 29)
Dim out As OutputStream
out = Serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
Sub Send_Text(mystring as String)
If connected Then
Dim a() As Byte = mystring.getBytes("UTF8")
Dim out As OutputStream
out = Serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
What are you trying to archieve?
Instead you can use a global variable.
But maybe a TIMER is wrong: what about a sub you can call to send anything?
B4X:Sub Send_Text(mystring as String) If connected Then Dim a() As Byte = mystring.getBytes("UTF8") Dim out As OutputStream out = Serial1.OutputStream out.WriteBytes( a, 0, a.Length) out.Flush End If End Sub
or similar
Thank you Klaus for the time and advice..Maybe use:
Dim a() As Byte = Array As Byte(0xAC, 0x05, 0xFE, 0x06, 0x03, 0x00, 0xCC, 0xD3)
instead of
Dim a() As Byte = Array As Byte(75, 28, 82, 158, 127, 0, 1, 29)
replace the numeric values by hex values, the hex values in the example above are not the same as your integer values.
Sub Process_Globals
Dim Timer1 as Timer
Dim m as Map
Dim CurrentState as String = "arry1"
End Sub
Sub AppStart (Args() As String)
m.Initialize
m.put("arry1", Array as Byte(75, 28, 82, 158, 127, 0, 1, 29))
m.put("array2", Array as Byte(83, 68, 82, 95, 127, 0, 0, 222))
m.put("arry3", Array as Byte(11, 22, 87, 127, 158, 0, 1, 29))
m.put("arry4", Array as Byte(59, 32, 82, 23, 95, 0, 65, 222))
m.put("array5", Array as Byte(57, 68, 54, 127, 127, 0, 0, 254))
Timer1.Initialize("Timer1", 50)
Timer1.Enabled = true
End Sub
Sub Timer1_Tick
If connected Then
Dim a() As Byte = m.Get(CurrentState) ' grabs the byte array with the current state
Dim out As OutputStream
out = Serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
' change direction
Sub Button1_Click
CurrentState = "array2"
End Sub
' change direction again
Sub Button2_Click
CurrentState = "arry3"
End Sub
...
Can't you just use a map and a global variable? Maybe you are overthinking this (or I misread what you want to do)
Something like (untested):
B4X:Sub Process_Globals Dim Timer1 as Timer Dim m as Map Dim CurrentState as String = "arry1" End Sub Sub AppStart (Args() As String) m.Initialize m.put("arry1", Array as Byte(75, 28, 82, 158, 127, 0, 1, 29)) m.put("array2", Array as Byte(83, 68, 82, 95, 127, 0, 0, 222)) m.put("arry3", Array as Byte(11, 22, 87, 127, 158, 0, 1, 29)) m.put("arry4", Array as Byte(59, 32, 82, 23, 95, 0, 65, 222)) m.put("array5", Array as Byte(57, 68, 54, 127, 127, 0, 0, 254)) Timer1.Initialize("Timer1", 50) Timer1.Enabled = true End Sub Sub Timer1_Tick If connected Then Dim a() As Byte = m.Get(CurrentState) ' grabs the byte array with the current state Dim out As OutputStream out = Serial1.OutputStream out.WriteBytes( a, 0, a.Length) out.Flush End If End Sub ' change direction Sub Button1_Click CurrentState = "array2" End Sub ' change direction again Sub Button2_Click CurrentState = "arry3" End Sub ...
Alwaysbusy
this is correct. It was just to show the principle.I suppose that your code was originally for B4J, because of AppStart.
just put all the code in AppStart into Activity_Create
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Serial Example
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#End Region
'Activity module
Sub Process_Globals
Dim Timer1 As Timer
Dim m As Map
Dim CurrentState As String = "arry1"
Dim serial1 As Serial
Dim connected As Boolean
End Sub
Sub Globals
Dim btnSend As Button
Dim txtLog As EditText
Dim txtSend As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
serial1.Initialize("Serial1")
Timer1.Initialize("Timer1", 200)
m.Initialize
m.put("arry1", Array As Byte(83, 68, 82, 158, 127, 0, 1, 29))
m.put("aray2", Array As Byte(83, 68, 82, 95, 127, 0, 0, 222))
m.put("arry3", Array As Byte(83, 68, 82, 127, 158, 0, 1, 29))
m.put("arry4", Array As Byte(83, 68, 82, 127, 95, 0, 0, 229))
m.put("arry5", Array As Byte(83, 68, 82, 127, 127, 0, 0,254))
Timer1.Initialize("Timer1", 50)
Timer1.Enabled = True
End If
Activity.LoadLayout("1")
Activity.AddMenuItem("Connect", "mnuConnect")
Activity.AddMenuItem("Disconnect", "mnuDisconnect")
End Sub
Sub Activity_Resume
If serial1.IsEnabled = False Then
Msgbox("Please enable Bluetooth.", "")
Else
serial1.Listen 'listen for incoming connections
End If
End Sub
Sub mnuConnect_Click
Dim PairedDevices As Map
PairedDevices = serial1.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1
l.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res = InputList(l, "Choose device", -1) 'show list with paired devices
If res <> DialogResponse.CANCEL Then
serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
End If
End Sub
Sub Serial1_Connected (Success As Boolean)
If Success Then
ToastMessageShow("Connected successfully", False)
Timer1.Enabled = True
connected = True
Else
connected = False
Timer1.Enabled = False
Msgbox(LastException.Message, "Error connecting.")
End If
End Sub
Sub mnuDisconnect_Click
serial1.Disconnect
connected = False
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnSend_Click
If connected Then
Dim a() As Byte = Array As Byte(83, 68, 82, 127, 95, 0, 0, 222)
Dim out As OutputStream
out = serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
Sub Timer1_Tick
If connected Then
Dim a() As Byte = m.Get(CurrentState) ' grabs the byte array with the current state
Dim out As OutputStream
out = serial1.OutputStream
out.WriteBytes( a, 0, a.Length)
out.Flush
End If
End Sub
What exactly does not work?
Put some Logs in the code to see what happens.
This means that you are using he 'old' way for MsgBox, you should use MsgboxAsync.Main - 46: Msgbox and other modal dialogs are deprecated. Use the async methods instead. (warning #34)
The Manifest is not up to date.The recommended value for android:targetSdkVersion is 29 (manifest editor). (warning #31)
Here, I am lost, I do not know what this does mean.Logger connected to: 431dc6bd
WARNING: linker: libpla.so: unused DT entry: type 0x6ffffffe arg 0x44d8
It seems that you are trying to use a very old example.
This means that you are using he 'old' way for MsgBox, you should use MsgboxAsync.
You must have a look HERE.
The Manifest is not up to date.
To update it, the simplest way is to open the Manifest Editor, delete delete everything, close it, confirm that you will keep the changes, reopen it you will get the most recent general Manifest settings.
Here, I am lost, I do not know what this does mean.
Set a breakpoint in the Timer1_Tick routine and check the value of the 'connected' value.
Are you sure that the devices are connected.
I have never used the Serial library in conjunction with Bluetooth, therefore no experience.
I had used the BLE2 library some time ago.
Several tests with a HC-05 Bluetooth module with an Arduino Uno, these examples are in the B4R Example projects booklet.
And another project for a friend reading weight values from a scale.
What is the make/model of the robot - is it an off-the-shelf robot, or one you've built? Those 8-byte packets look like they're from a game controller.Trying to control a robot by Bluetooth through Android app.
I made program for a friend to communicate with between a B4X program and kitchen scale with BLE2 library.Those 8-byte packets look like they're from a game controller.
I made program for a friend to communicate with between a B4X program and kitchen scale with BLE2 library.
The communication was always with 8 byte packets.
The first two bytes were always the same and the last byte was the checksum.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?