Android Tutorial MQTT Chat with Auto Discovery

This is an extension to the MQTT chat room example: https://www.b4x.com/android/forum/threads/b4x-mqtt-chat-room.61568/#content
It is a chat room where one of the devices acts as a server and the others as clients.

SS-2017-01-30_10.56.47.jpg


The difference in this example is that the clients don't need to enter the server ip address.
All devices listen to UDP packets on port 51049.
The server broadcasts its ip address every second. It is done by sending a UDP packet to the broadcast address.
The result is that any device listening on the correct port will receive the message.
The "Connect As Client" button will be enabled when the server ip address is discovered (and the name field is not empty).

Finding the broadcast address:
B4A: https://www.b4x.com/android/forum/threads/75694/#content
B4J / B4i: UDPSocket.GetBroadcastAddress

Note that it can take a few seconds before the broadcast message arrives.
 

Attachments

  • MqttChatWithAutoDiscovery.zip
    11.7 KB · Views: 3,311
Last edited:

james_sgp

Active Member
Licensed User
Longtime User
I'm getting the dreaded "invokedynamic requires" error when trying to install the demo of MQTT "Simple Chat with Autodiscovery", I`m using jMQTT V1.00, jMQTTBroker2 V2.02.

Any suggestions, please...
 

james_sgp

Active Member
Licensed User
Longtime User
Erel, great thanks as usual...i'll give it a try later.

James


Yes, that worked. Thanks again Erel.
 
Last edited:

james_sgp

Active Member
Licensed User
Longtime User
So I have MQTT running on 2 Samsung phones now, one is the Server and one is client; however when I enter a name, and try to connect to the server I get an error message on the phone saying "Error connecting: (errnoException)", in B4A Logs, it just says Discovered 192.168.1.186, then next line says "Connected: false"


James
 

james_sgp

Active Member
Licensed User
Longtime User
Ok, had to do an Android update on one of the phones, now it works. Sorry for the trouble.

James
 
Top