MqttException(0) - AndroidThings上的java.net.NoRouteToHostException

时间:2016-12-26 18:26:56

标签: android python mqtt paho android-things

我正在尝试仍处于预览模式的Android Things 通过代码与wifi和本地网络进行通信,面临许多挑战。
其中一个例子就是将RPI3上的Android Thing作为MQTT经纪人 我不确定问题是与MQTT代码有关还是与Android Things有关。

有人可以帮我在这里找出问题吗? 这是我的代码:

package com.example.androidthings.myproject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManagerService;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.io.IOException;

    public class MainActivity extends Activity implements MqttCallback{
    private static final String TAG = MainActivity.class.getSimpleName();

    public static final String MOTOR_A_PIN_1 = "BCM21"; //physical pin #40
    public static final String MOTOR_A_PIN_2 = "BCM20"; //physical pin #38
    public static final String MOTOR_B_PIN_1 = "BCM24"; //physical pin #18
    public static final String MOTOR_B_PIN_2 = "BCM23"; //physical pin #16

    private Gpio motorAPin1;
    private Gpio motorAPin2;
    private Gpio motorBPin1;
    private Gpio motorBPin2;

    private MqttClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate.....Motor MQTT");

        try {
            MemoryPersistence persistance = new MemoryPersistence();
            client = new MqttClient("tcp://127.0.0.1:1883", "AndroidThingBroker", persistance);
            client.connect();

            String topic = "topic/androidthings";
            int qos = 1;
            client.subscribe(topic, qos);

        } catch (MqttException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        PeripheralManagerService service = new PeripheralManagerService();
        try {

            // Create GPIO connection for L293D (Motor will run through L293D).
            motorAPin1 = service.openGpio(MOTOR_A_PIN_1);
            // Configure as an output.
            motorAPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            // Create GPIO connection for L293D (Motor will run through L293D).
            motorAPin2 = service.openGpio(MOTOR_A_PIN_2);
            // Configure as an output.
            motorAPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            // Create GPIO connection for L293D (Motor will run through L293D).
            motorBPin1 = service.openGpio(MOTOR_B_PIN_1);
            // Configure as an output.
            motorBPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            // Create GPIO connection for L293D (Motor will run through L293D).
            motorBPin2 = service.openGpio(MOTOR_B_PIN_2);
            // Configure as an output.
            motorBPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

        } catch (IOException e) {
            Log.e(TAG, "Error on PeripheralIO API", e);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");

        if (motorAPin1 != null) {
            try {
                motorAPin1.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }

        if (motorAPin2 != null) {
            try {
                motorAPin2.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }

        if (motorBPin1 != null) {
            try {
                motorBPin1.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }

        if (motorBPin2 != null) {
            try {
                motorBPin2.close();
            } catch (IOException e) {
                Log.e(TAG, "Error on PeripheralIO API", e);
            }
        }
    }

    @Override
    public void connectionLost(Throwable cause) {
        Log.d(TAG, "connectionLost....");
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String payload = new String(message.getPayload());
        Log.d(TAG, payload);
        // do stuff
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        Log.d(TAG, "deliveryComplete....");
    }
}

错误:

12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err: 
Unable to connect to server (32103) - java.net.ConnectException: Connection refused
12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err:     at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.lang.Thread.run(Thread.java:761)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: Caused by: java.net.ConnectException: Connection refused
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.PlainSocketImpl.socketConnect(Native Method)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err:     at java.net.Socket.connect(Socket.java:586)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err:     at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err:  ... 2 more

**编辑**
将IP地址更改为127.0.0.1,现在接收不同的错误堆栈。我已经更新了上面的错误日志。

我很糟糕...... MQTT经纪人失败了。我以为我可以在设备上创建一个代理。然后我意识到它应该在服务器上运行,而我在初始测试期间启动的那个已经关闭了。
当我使用Mosquitto启动代理服务器时,我现在能够从Android连接到它:D

1 个答案:

答案 0 :(得分:0)

如果adb shell然后ping正在运行,则意味着您的Android Things主板具有互联网连接,很可能不是您的问题。

查看堆栈跟踪

 client = new MqttClient("tcp://192.168.1.4:1883", "AndroidThingBroker", persistance);
 client.connect();

是抛出错误的代码,以缩小您可以在您的计算机上创建一个简单的Android应用程序甚至只是一个java main方法并解决路由问题的问题。也许ip是错误的,但它似乎不是一个Android事件问题(或者至少让你的代码作为一个Java应用程序工作,以尽量减少可能性)。

我也会删除此代码:

 catch (Exception e) {
        e.printStackTrace();
    }

这样您就不会捕获异常 - 您希望应用程序尽快中断,以便您可以看到路径错误,而不是原始错误的副作用。