Reputation: 390
Does anyone know of an MQTT broker that runs on an Android smartphone? I tried to Google and found nothing, and on the app store there seems to be only one app with just 10 downloads, so I'm not sure how well it works.
Upvotes: 11
Views: 39550
Reputation: 343
build.gradle
android{
packagingOptions {
resources.excludes.add("META-INF/*")
}
}
dependencies {
implementation 'io.moquette:moquette-broker:0.17'
}
Java code
public class MqttServer {
public static final String TAG = MqttServer.class.getSimpleName();
public MqttServer(Context context) throws Exception {
Server server = new Server();
String rootFolder = getExternalStorageDirectory(context);
String path = rootFolder + "/" + BrokerConstants.DEFAULT_MOQUETTE_STORE_H2_DB_FILENAME;
MemoryConfig memoryConfig = new MemoryConfig(new Properties());
memoryConfig.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, path);
server.startServer(memoryConfig);
Log.i(TAG, "[MqttServer] MQTT server started. MQQT Url= tcp://localhost:"+server.getPort());
}
public static String getExternalStorageDirectory(Context context) {
return context.getExternalFilesDir("").getPath();
}
}
Upvotes: 0
Reputation: 379
You can run the mosquitto mqtt broker within the Termux terminal.
Install mosquitto
pkg install mosquitto
Start mosquitto within the terminal
mosquitto
That's it. The server will listen on the default port 1883. Configuration see at mosquitto.
Upvotes: 3
Reputation: 2064
I have developed an App specifically for this , please download here -
It has inbuilt broker and client too..all for free , connect your devices to android phone via hotspot or wifi.
https://play.google.com/store/apps/details?id=server.com.mqtt
Upvotes: 7
Reputation: 604
Add these dependencies to the gradle
dependencies{
compile 'io.moquette:moquette-netty-parser:0.8.1'
compile 'io.moquette:moquette-broker:0.8.1'
compile 'io.moquette:moquette-parser-commons:0.8.1'
}
And use
io.moquette.server.Server server = new io.moquette.server.Server();
server.startServer();
to start broker server. the default URI is tcp://localhost:1883
For me server.startServer();
gave me exception as it is unable to create a file BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME
.
So, I changed the destination of the BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME
using this code below code and it worked for me.
try {
MemoryConfig memoryConfig = new MemoryConfig(new Properties());
memoryConfig.setProperty(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME);
server.startServer(memoryConfig);
// server.startServer();//is not working due to DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME;
Log.d(TAG,"Server Started");
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e){ e.printStackTrace(); }
And Use Paho libraries for android
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
To create a client and connect to tcp://localhost:1883
and subscribe for a topic and start publishing and receiving messages.
Upvotes: 19
Reputation: 859
Here is an MQTT broker library I have adapted to Android: https://github.com/interaktionsbyran/moquette You'll have to make your own Android app though, it is just a library.
Upvotes: 2