Android Question ConnectAndReconnect - Mqtt.close problem

amorosik

Expert
Licensed User
App Android with mqtt connection to Mosquitto on pc
I use the code like ConnectAndReconnect Erel example
The question is: how to close connection faster than 30 sec?

I tried Erel's suggestion on this post (change mqtt client_id) but without success
(on row 'mqtt_client.Connect2(mo)' there an error 'Connection not close')

The final question is: how can I quickly reactivate a connection when the broker is back up and running?

B4X:
Sub ConnectAndReconnect
    Do While flag_connetti_mqtt
   
        If mqtt_client.IsInitialized Then mqtt_client.Close
'        Do While mqtt_client.IsInitialized
'            Sleep(100)
'            Loop
       
        mqtt_client_id="MyApp_" & Rnd(0, 999999)
        mqtt_client.Initialize("EventMqtt", mqtt_uri, mqtt_client_id)
       
        mqtt_username="nome_utente_mqtt"
        mqtt_password="password_mqtt"
       
        Dim mo As MqttConnectOptions
        mo.Initialize(mqtt_username, mqtt_password)
        mo.SetLastWill("smartphone/sconnesso" , serializator.ConvertObjectToBytes( mqtt_client_id), 0, False)
        mqtt_client.Connect2(mo)
       
        Wait For EventMqtt_Connected (Success As Boolean)
       
        If Success Then
            mqtt_connected=True
            Dim stringa_connected As String= "COM=MQTT_CONNESSO;"
           
            Try
                mqtt_client.Subscribe("Cons2MyApp",0)
                mqtt_client.Publish2("MyApp2console", serializator.ConvertObjectToBytes(stringa_connected), 0, False)
                Catch
                Log(LastException)
                End Try

            Do While flag_connetti_mqtt And mqtt_client.Connected
                Log ("Ping verso consolle")
               
                Try
                    Catch
                    Log(LastException)
                    End Try
               
                Sleep(1000)
                Loop
               
            mqtt_connected=False
            Else
            mqtt_connected=False
            End If
           
        Loop
End Sub
 
Top