Android Question MQTT SimpleChat unable to connect to hiveMQ cloud

yfleury

Active Member
Licensed User
Longtime User
I set name and host for local network work well one device as server and another as client

But I want to understand how to connect to hiveMQ with 2 device as client.

I set Host to XXXXXXXXXXXXXXXXXXXXXXXX.s1.eu.hivemq.cloud
I set
Dim mo As MqttConnectOptions
mo.Initialize("myname", "Mypass")
I try port 8883 and 8884
Then I receive this error
Error connecting: (EOFException) java.io.EOFException

What I doing wrong?
 

Magma

Expert
Licensed User
Longtime User
Hi there....

if you created acccount need at SERVERURI:

ssl://xxxxxxxxxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud:8883

and because using ssl:
B4X:
TrustAll(mo)

'Depends on JavaObject and jNet or Net libraries
Sub TrustAll (mo As MqttConnectOptions)
    Dim SSLContext As JavaObject
    SSLContext = SSLContext.InitializeStatic("javax.net.ssl.SSLContext").RunMethod("getInstance", Array("TLS"))
    Dim tm As CustomTrustManager
    tm.InitializeAcceptAll
    SSLContext.RunMethod("init", Array(Null, tm, Null))
    Dim jmo As JavaObject = mo
    jmo.RunMethod("setSocketFactory", Array(SSLContext.RunMethod("getSocketFactory", Null)))
End Sub
 
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
I change the link to ssl://
I add the sub
I add javaObject and Net libraries
Where i put TrustAll(mo)
I try it that sub
B4X:
Public Sub ConnectTo(Host As String, Name As String)
    currentName = Name
    isServer = Host = "127.0.0.1"
    If isServer Then
        If brokerStarted = False Then
            broker.Start
            brokerStarted = True
        End If
        users.Clear
'        Host = "127.0.0.1"
    End If
    If connected Then client.Close
    client.Initialize("client", $"ssl://${Host}:${port}"$, "android" & Rnd(1, 10000000))
    Dim mo As MqttConnectOptions
    mo.Initialize("yves1", "lespommes")
TrustAll(mo)                              '<<<<<<<<<<<<<<<<<<<--------------'
    'this message will be sent if the client is disconnected unexpectedly.
    mo.SetLastWill("all/disconnect", serializator.ConvertObjectToBytes(currentName), 0, False)
    client.Connect2(mo)
End Sub
I have the same error
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Well i don't know exactly what is your problem - because not having the full project... but the following code with direct user/pass/url of server works!
B4X:
Public Sub ConnectTo(Host As String, Name As String) 'not using host and name...
    Log("try to connect...")
    If mqtt.Connected=True Then    mqtt.close
        Do While mqtt.IsInitialized
            Sleep(100)
        Loop
    mqtt.Initialize("mqtt", "ssl://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.s2.eu.hivemq.cloud:8883",  "android1")  'have in mind that "mqtt" here is event!!!!
    Dim mo As MqttConnectOptions
    mo.Initialize("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    TrustAll(mo)                              '<<<<<<<<<<<<<<<<<<<--------------'

    mqtt.Connect2(mo)
    Wait For mqtt_Connected (Success As Boolean)
    If Success Then
        Log("SUPER!!!!")
        'here is a good place for subscribe...
        '....
        '....
        
        Do While mqtt.Connected
                Try
                Dim CX As B4XSerializator
                Dim res As Int=1821 ' a number or anything you want
                    mqtt.Publish2("ping", CX.ConvertObjectToBytes(res), 0, False)
                Catch
                    Log(LastException)
                End Try
            Sleep(1000)
        Loop
    Else
        Log(Success)
    End If
End Sub

Sub mqtt_Connected (Success As Boolean)
    Log(Success)
....

End Sub

Sub mqtt_Disconnected
    Log("disconnected-mqtt")
...
End Sub

Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
....
 
Upvote 0
Top