Android Question MQTT payload type

August Jimenez

Member
Licensed User
Hi,

I'm doing some test with a MQTT broker, using the MQTTBox tool, to verify all is working well, before use my APP. The topic to publish and the payload used are in the following screenshot:

upload_2018-10-18_18-5-24.png


With this tool, all is working fine. When I use the APP, I can connect to the broker perfectly, but the publish topic does not arrive correctly. My piece of code is the following:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    If FirstTime Then
       
        mqtt.Initialize("mqtt", serverURI, "xxxxxxx")

        Dim mo As MqttConnectOptions
       
        mo.Initialize("xxxxxxxx", "yyyyyyyy")

        Dim jo As JavaObject = mo

        jo.RunMethod("setSocketFactory", Array(CreateTrustAllSSLSocketFactory))

        mqtt.Connect2(mo)
       
    End If
   
End Sub

Sub CreateTrustAllSSLSocketFactory As Object
   
    Dim tm As CustomTrustManager
   
    tm.InitializeAcceptAll
   
    Dim SSLContext As JavaObject
   
    SSLContext = SSLContext.InitializeStatic("javax.net.ssl.SSLContext").RunMethod("getInstance", Array("TLS"))
   
    SSLContext.RunMethod("init", Array(Null, tm, Null))
   
    Dim Factory As JavaObject = SSLContext.RunMethod("getSocketFactory", Null)
   
    Return Factory
   
End Sub

Sub mqtt_Connected (Success As Boolean)
   
    If Success = False Then 
       
        Log("Error connecting: " & LastException)
       
    Else
       
        log("Connected")

        mqtt.Publish("v/a/g/141414141414/s/temperature-141414141414-1", serializator.ConvertObjectToBytes("1539878280000,25.5"))

    End If
   
End Sub

As can be seen in the screenshot, the payload should be "Strings / JSON / XML / Characters", but in the "Publish" method of the jMQTT library, the payload must be "Byte".

How can I send this kind of payload?

Thanks and regards
 

Attachments

  • upload_2018-10-18_18-5-24.png
    upload_2018-10-18_18-5-24.png
    24.8 KB · Views: 318
Top