B4A Library MQTT - MQ Telemetry Transport for B4A


This library was made based on MQTT Protocol

Installation instructions:
- Copy the .xml and .jar(s) files to your custom libraries folder


Version history:
V1.0.0 (First public release - Donationware):
- Initial version

V1.0.2 (Release - Donationware):
- Implement method to get independence of threads

V1.0.4 (Release - Donationware):
- Implement method to subscribe a topic with QoS

V1.0.5 (Release - Donationware):
- Now can be used isConnected method "BEFORE" the first connection

(Release - Donationware):
- not published

V1.0.7 (Release - Donationware):
- New methods: Publish2, Subscribe3, Subscribe4
This new methods return the token, then after when arrived or publish the messages, you can compare

Alberto Iglesias - [email protected]
Version: 1.07
  • MQTT
    • connectionlost ( )
    • deliverycomplete (Token As String)
    • messageArrived (Topic As String, Message As String)
    • onConnect (Status as boolean As )
    • onDisconnect (Status as boolean As )
    • onInitialized ( )
    • CleanSession As Boolean
    • JavaDebug As Boolean
    • KeepAliveInterval As Int
    • QoS_AtLeastOnce As Int
    • QoS_AtMostOnce As Int
    • QoS_ExactlyOnce As Int
    • generatedClientId As String
    • About As String
    • Connect (broker As String, ClientID As String, Username As String, Password As String) As Boolean
    • Disconnect As Boolean
    • Initialize (eventname As String)
    • Publish (Topic As String, Message As String, Qos As Int, Retained As Boolean)
    • Publish2 (Topic AsString, Message AsString, Qos AsInt, Retained AsBoolean)
    • Subscribe (Topic As String)
    • Subscribe2 (Topic As String, QoS as int)
    • Subscribe3 (Topic As String)
    • Subscribe4 (Topic As String, QoS as int))
    • Unsubscribe (Topic As String)
    • Version As String
    • isConnected As Boolean
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.INTERNET



How works the MQTT protocol

Communication Infrastructure


Mosquitto Broker: http://mosquitto.org/

* This version is fully functional, the only one difference is when publishing a message the text "Donationware" is delivery with your message.
Please consider a simple €10 donation and I send to you the Library without "Donationware" text in a publish method.

Already available in B4J too:

Already available in B4i too:



  • MQTTB4ASample.zip
    7.8 KB · Views: 813
  • MQTT Library1.0.2.zip
    162 KB · Views: 495
  • MQTTSampleActivity.zip
    8.2 KB · Views: 622
  • SampleSubscribe2.zip
    8.3 KB · Views: 585
  • MQTT Library1.0.4.zip
    154.6 KB · Views: 427
  • MQTT Library1.0.5.zip
    154.9 KB · Views: 467
  • SampleIsConnected.zip
    8.3 KB · Views: 660
  • SampleWithToken.zip
    8.2 KB · Views: 459
  • MQTT Library1.0.7.zip
    155.6 KB · Views: 848
Last edited:


Active Member
Licensed User
Longtime User
Hi, I'm very interested in this lib.
Do You think i can work with B4J?

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User

The MQTT protocol says:

" The length is limited to 64k but within that there are no limits to the number of levels in a topic tree.
  • There can be any number of root nodes; that is, there can be any number of topic trees."
But the MQTT was constructed to be lightweight, to be fast!

I think works, but you can try...

And for B4i, YES, I think in construct a library in January, because in this holydays I can´t :-( Sorry.....



Well-Known Member
Licensed User
Longtime User
Interesting and works like a breeze. I have a broker running on CentOS VPS and it was only a few minutes work.

The B4A sample seems to have some threading problems, I cannot intercept the message and bind it to a view. Any suggestions?

Should I put the sample in a service?


Well-Known Member
Licensed User
Longtime User
In objMqtt_messageArrived(Topic As String, Message As String)

i'm trying to put the received Message in an edittext on the layout, this is not allowed and i'm getting an error about main threads. Logging it works fine but for the rest I cannot do anything.

I'm not using any other libraries just your stuff.


Well-Known Member
Licensed User
Longtime User
Works great but what i'm missing is a Quality of Service flag in the subscribe method. I see you have it in publish but not in subscribe.


Well-Known Member
Licensed User
Longtime User
Great, I will make a donation a.s.a.p. MQTT is a huge benefit over Google Cloud Messaging which sucks in my opinion. JSON over MQTT is perfect for many scenario's, instead of polling for new data a server can update clients through topics which is much better.

Aidan Ruff

Licensed User
Longtime User
Hola Alberto
I just sent you 10 euros - nice work, thanks. If you get a chance to send me the full version without the added text, I'd appreciate it!