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: 864
  • MQTT Library1.0.2.zip
    162 KB · Views: 552
  • MQTTSampleActivity.zip
    8.2 KB · Views: 675
  • SampleSubscribe2.zip
    8.3 KB · Views: 625
  • MQTT Library1.0.4.zip
    154.6 KB · Views: 469
  • MQTT Library1.0.5.zip
    154.9 KB · Views: 509
  • SampleIsConnected.zip
    8.3 KB · Views: 716
  • SampleWithToken.zip
    8.2 KB · Views: 505
  • MQTT Library1.0.7.zip
    155.6 KB · Views: 918
Last edited:


Licensed User
Longtime User
Hi Alberto
Can you help me ? I already use mqtt with MQTTlens, raspberry, PIC's, ESP8266, so I believe my environment is ok, but when I try to compile your sample for Android, i get this message?

Compiling generated Java code: Error
Cannot find C:\program files (x86)\Basic4android\libraries\org.eclipse;paho.client.mqttv3-1.0.1.jar

Thank You


Licensed User
Longtime User
It is running now.
Very simple.
Just a question ? Where are you located ? I saw an answer in good portuguese. Are you in Brazil ??
I am asking because my credit card only accept US$ convertions, not euros.. Is there any way I can pay for it in US$ directly ?


Active Member
Licensed User
Longtime User
Hi Alberto.
I set your 1.07 library as you suggested.
Im trying to use "CloudMQTT"

Dim strBroker As String    = "m10.cloudmqtt.com:17612"
   Dim strUsername As String  = "XXXXXXXX"
   Dim strPassword As String  = "XXXXXXXXX"
   Dim bRet As Boolean = objMqtt.Connect(strBroker,"1",strUsername,strPassword)

"1" might be Client ID, is right?
But each time i clicked Connect button i received this log error.
Could you suggest me what am i missing?

** Activity (main) Create, isFirst = true **
MQTT About = MQTT Library - By Alberto Iglesias ([email protected])
MQTT Version = 1.0.7 ** DONATIONWARE **
** Activity (main) Resume **
Error occurred on line: 49 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:457)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
at com.visualnet.mqtt.Connect(mqtt.java:313)
at com.visualnet.mqtttest.main._button1_click(main.java:433)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

EDIT: Same issue with my RPi as MQTT broker running mosquitto at ip:1883.
Last edited:


Active Member
Licensed User
Longtime User
Now works well!!!
I need to compare your last Sample with the old one.
More questions when i find differences.


New Member
Licensed User
Longtime User
Hi Alnerto, I like your library, I test here: http://cloud.iot-playground.com/ and work very well when I publish and subscribe.

I will a make a donation later next week, when I do this, you still send me a library without "Donationware" right ?

Thanks in advance!

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello my friend, I'm not using B4x anymore, you can compare with this library, I create mine based on this: org.eclipse.paho.client.mqttv3



Licensed User
Hello my friend, I'm not using B4x anymore, you can compare with this library, I create mine based on this: org.eclipse.paho.client.mqttv3

Also jMqtt is based on this project, then is difficult find pro/cons of jMqtt vs your library
"..I'm not using B4x anymore.." can we know if you have moved to other developer enviroment?


Licensed User
Hi Alberto, could you tell me if your library, based on the Eclipse Paho project, allows automatic client reconnection, and high availability (alternative server use if main not available)?