Android应用程序中基于本地代理(MQTT)的发布机制

时间:2017-01-12 16:40:44

标签: android node.js mqtt

提到问题Local broker (MQTT) based Publish/Subscribe using Android Application。在我目前的项目中,我想使用基于本地代理的MQTT实现将数据从Android应用程序发送到Node.js。

根据上述问题中提供的解决方案,我使用相同的解决方案将数据从Android应用程序(本地代理的tcp://192.168.0.105 Android API)发送到Node.js app mqtt://192.168.0.105(节点) .js侧面API用于本地代理的ip),但我无法将数据从Android发送到Node.js应用程序。这里192.168.0.105是本地代理的IP地址。如果我在Node.js端使用mqtt://test.mosquitto.org:1883 API,在客户端使用tcp://test.mosquitto.org:1883 API而不是完美运行。

使用基于本地代理的API,整个项目不会显示任何错误。分享android项目的日志以获得更好的清晰度。它还使用以下日志(日志中的第1行)打印tempValue。

01-12 22:00:07.348: I/System.out(23135): tempValue is41.0in Android Smart Home App
01-12 22:00:07.349: W/System.err(23135): Client is not connected (32104)
01-12 22:00:07.349: W/System.err(23135):    at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:27)
01-12 22:00:07.349: W/System.err(23135):    at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:101)
01-12 22:00:07.349: W/System.err(23135):    at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:119)
01-12 22:00:07.349: W/System.err(23135):    at org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
01-12 22:00:07.350: W/System.err(23135):    at iotsuite.pubsubmiddleware.MQTTPublisher.publish(MQTTPublisher.java:34)
01-12 22:00:07.350: W/System.err(23135):    at iotsuite.pubsubmiddleware.PubSubMiddleware.publish(PubSubMiddleware.java:89)
01-12 22:00:07.350: W/System.err(23135):    at framework.SmartHomeApp.SetTempFromGUI(SmartHomeApp.java:40)
01-12 22:00:07.350: W/System.err(23135):    at logic.LogicSmartHomeApp$1.onNewSetTempCommand(LogicSmartHomeApp.java:56)
01-12 22:00:07.350: W/System.err(23135):    at deviceImpl.AndroidSmartHomeApp.onClick(AndroidSmartHomeApp.java:77)
01-12 22:00:07.350: W/System.err(23135):    at android.view.View.performClick(View.java:5207)
01-12 22:00:07.350: W/System.err(23135):    at android.view.View$PerformClick.run(View.java:21168)
01-12 22:00:07.350: W/System.err(23135):    at android.os.Handler.handleCallback(Handler.java:746)
01-12 22:00:07.350: W/System.err(23135):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 22:00:07.350: W/System.err(23135):    at android.os.Looper.loop(Looper.java:148)
01-12 22:00:07.350: W/System.err(23135):    at android.app.ActivityThread.main(ActivityThread.java:5443)
01-12 22:00:07.350: W/System.err(23135):    at java.lang.reflect.Method.invoke(Native Method)
01-12 22:00:07.350: W/System.err(23135):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
01-12 22:00:07.350: W/System.err(23135):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-12 22:00:07.350: I/System.out(23135): Publishing data in Smart Home App
01-12 22:00:23.332: W/System(24876): ClassLoader referenced unknown path: /system/priv-app/Settings/lib/arm
01-12 22:01:45.401: W/System(24934): ClassLoader referenced unknown path: /data/app/com.goibibo-1/lib/arm
01-12 22:01:45.634: W/System(24934): ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000011/n/armeabi-v7a
01-12 22:01:45.635: W/System(24934): ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000011/n/armeabi

tcp://192.168.0.105mqtt://192.168.0.105http://192.168.0.105 API与Node.js本地代理实现一起使用。应用程序的Node.js代码如下所示:

    var mqtt = require('mqtt');
var client = mqtt.connect('http://192.168.0.105');
client.subscribe('tempMeasurement'); 
console.log("waiting for tempValue");
client.on('message', function(topic, payload) {     
        if (topic.toString() == "tempMeasurement") {  
            console.log("Mesage Received "+ payload.toString());
        }           
});  

Android应用程序的代码与我在上述问题中发布的代码相同。我是否错过了Hardillb在此实现中提供的解决方案?

0 个答案:

没有答案