Not sure how to structure a B4J program that will require MQTT async subscription updates that will need to be reflected on the active form.
I can have this code on Main:
but where is it called in B4J without a B4A background service running and only one of many forms active at a time?
If ConnectAndReconnect is placed in:
Edit: I do get repeat execution after sleep in the do while loop.
However is this the best way to structure such an app?
Also do I make public and call:
From every form?
I can have this code on Main:
B4X:
Sub ConnectAndReconnect
Do While working
If mqtt.IsInitialized Then mqtt.Close
mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4X" & Rnd(0, 999999999))
Dim mo As MqttConnectOptions
mo.Initialize(username, password)
Log("Trying to connect")
mqtt.Connect2(mo)
Wait For Mqtt_Connected (Success As Boolean)
If Success Then
Log("Mqtt connected")
Do While working And mqtt.Connected
mqtt.Publish2("ping", Array As Byte(0), 1, False) 'change the ping topic as needed
Sleep(5000)
Loop
Log("Disconnected")
Else
Log("Error connecting.")
End If
Sleep(5000)
Loop
End Sub
but where is it called in B4J without a B4A background service running and only one of many forms active at a time?
B4X:
Sub Process_Globals
Private working As Boolean = True
Private mqtt As MqttClient
End Sub
Sub Service_Create
working = True
mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4A" & Rnd(0, 999999999))
ConnectAndReconnect
End Sub
If ConnectAndReconnect is placed in:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
WelcomeForm.Show
ConnectAndReconnect
End Sub
Edit: I do get repeat execution after sleep in the do while loop.
However is this the best way to structure such an app?
Also do I make public and call:
B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
...
From every form?
Last edited: