有人知道有没有在安卓手机上运行的MQTT代理吗?我尝试了谷歌搜索但是没有找到,应用商店中只有一个下载量仅为10次的应用程序,不确定它的效果如何。
将这些依赖项添加到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'
}
并使用
io.moquette.server.Server server = new io.moquette.server.Server();
server.startServer();
启动代理服务器。默认 URI 为 tcp://localhost:1883
对我来说,server.startServer();
报错了,因为它无法创建文件 BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME
。
所以,我使用下面的代码更改了 BrokerConstants.DEFAULT_MOQUETTE_STORE_MAP_DB_FILENAME
的目标位置,并且这对我起作用了。
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(); }
同时在安卓设备上使用Paho库。
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
创建一个客户端并连接到 tcp://localhost:1883
,订阅一个主题并开始发布和接收消息。使用Android的moquette库。io.moquette.server.Server server = new io.moquette.server.Server(); server.startServer();
来启动服务,请在服务或线程中运行此代码。 - Tej我专门为此开发了一款应用程序,请在此处下载:
它还具有内置的代理和客户端,全部免费。通过热点或WiFi将您的设备连接到Android手机。
https://play.google.com/store/apps/details?id=server.com.mqtt