如何使用Eclipse paho连接到Android上的mqtt代理

时间:2015-03-18 15:39:00

标签: android mqtt paho

我想编写一个连接到BLE设备的小型IoT代理,并通过MQTT将消息转发给MQTT代理。似乎唯一存在的Android MQTT服务是Eclipse Paho。我正在尝试这个,但没有收到任何消息。而是抛出异常....

MqttAndroidClient client = new MqttAndroidClient(getApplicationContext(),"tcp://xx.xx.xx.xx:1883", "androidClient");
    try {
        client.connect();
        client.publish("box/1", new MqttMessage("blubb".getBytes()));
    } catch (MqttException e) {
        e.printStackTrace();
    }

错误是这样的:

03-18 16:35:03.135  28618-28618/com.hybris.labs.moto E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hybris.labs.moto, PID: 28618
java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttDeliveryToken org.eclipse.paho.android.service.MqttService.publish(java.lang.String, java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage, java.lang.String, java.lang.String)' on a null object reference
        at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:812)
        at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:668)
        at org.eclipse.paho.android.service.MainActivity$1.onServiceConnected(MainActivity.java:169)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1203)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1220)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)

有什么想法吗?

0 个答案:

没有答案