Android Question B4A MQTT Error in TCP

nino.escalera

New Member
Im tring to connect mqtt to tcp://broker.hivemq.com:1883

App works fine in windows emulator (MEMU) but running this on real phone (Xiaomi Note 13) gets an error

B4X:
(ErrnoException) android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)

While connecting to secured mqtt like ssl://abc1234.eu.hivemq.cloud:8883 works fine is both cases

Im using B4XPages, B4A 13.40

B4X:
AddPermission(android.permission.INTERNET)
SetApplicationAttribute(android:usesCleartextTraffic, true)

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public Mqtt As MqttClient
    Private Server As String = "tcp://broker.hivemq.com:1883"
    Private TimerConnect As Timer
End Sub

Sub Service_Create
    TimerConnect.Initialize("TimerConnect", 1500)
    Mqtt.Initialize("MC", Server, DateTime.Now.As(String) & Rnd(1000, 9999))
    TimerConnect_Tick
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
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

Private Sub MC_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)
        TimerConnect.Enabled = True
    Else
        Log("MQTT Connected")
        Mqtt.Subscribe("mytopic", Mqtt.QOS_2_EXACTLY_ONCE)
    End If
End Sub

Private Sub MC_Disconnected
    Log("MQTT Disconnected")
    TimerConnect.Enabled = True
End Sub

Private Sub MC_MessageArrived (SenderTopic As String, Payload() As Byte)
    Dim dat As String = BytesToString(Payload, 0, Payload.Length, "UTF-8")
    Log(dat)
End Sub

Private Sub TimerConnect_Tick
    TimerConnect.Enabled = False
    If Mqtt.Connected = False Then
        Mqtt.Connect
    End If
End Sub
 
Top