1
votes

I have an ActiveMQ broker in my network listening on 1883 (mqtt). If I connect to that broker using a simple Java application and the mqtt-client-0.4.0.jar library everything works.

Now I want to connect to that broker with an Android device. Unfortunately I just can't manage it to get a successful connection. I'm always getting the following error:

03-23 11:08:15.679 24572-24572/com.my.package E/WorkerMQTT: onFailure: Unable to connect to server (32103) - java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.687 24572-24572/com.my.package W/System.err: Unable to connect to server (32103) - java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err:     at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err:     at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err:     at java.lang.Thread.run(Thread.java:856)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:224)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at java.net.Socket.connect(Socket.java:842)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     ... 2 more
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: Caused by: libcore.io.ErrnoException: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:208)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err:     ... 8 more

I'm using and Android Service which calls a MqttWorker extends Thread{.

Also as library I'm using:

compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
      exclude module: 'support-v4'
}

Following the code of my worker:

@Override
public void run() {
String clientId = "AndroidSub_7";

    try {
        this.client =
            new MqttAndroidClient(this.context, "tcp://192.192.192.10:1883", clientId);

        this.options = new MqttConnectOptions();
        this.options.setWill("clients/clienterrors", "crashed".getBytes(),2,true); // handle uncleanly closed subscribers

        this.client.setCallback(new ClientCallbacks()); // set new callbacks
        this.client.connect(options);

        IMqttToken conToken;

        conToken = client.connect(options, this.context, new IMqttActionListener() {
        @Override
        public void onSuccess(IMqttToken asyncActionToken) {
            Log.d(TAG, "onSuccess");
        }

        @Override
        public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
            Log.e(TAG, "onFailure: " + exception);
            exception.printStackTrace();
        }

        });
    } catch (MqttException e) {
        Log.e(TAG,"Connection to broker failed:" + e);
    }
}

What I tried so far:

  1. Made sure not to use the loopback address (a lot of threads here are about this)
  2. Made sure that i have the required permissions set in the AndroidManifest.xml WAKE_LOCK /INTERNET / WRITE_EXTERNAL_STORAGE / ACCESS_NETWORK_STATE
  3. I created a simple threaded class to check if I can connect to the port using a socket. It succeeded.
  4. It doesn't matter if I'm using the MqttAndroidClient or MqttClient. The error is the same.

I hope someone can help me. I'm running out of ideas how to fix this.

1
Is this a real Android device or the emulator?hardillb
The no route to host error implies what ever network your device/emulator is connected to has no way to reach the broker. Please give more details about your network setup (where is the broker hosted, which network the client is connected to)hardillb

1 Answers

1
votes

I used a real Android device. To get more information about the network, I connected to the Android device over adb shell. From the ping command i got the following From 192.192.192.1 icmp_seq=2 Destination Net Unreachable. Strange so, that it still worked using the socket test. Overall, I restarted the device and now it works. The answer is maybe not that satisfactory, but at least it works now.