Search code examples
android-studiomqtt

MqttAndroidClient.connect() to Raspberry Pi


I am trying to build an app in android studio that makes mqtt publishes to the broker which is on a Raspberry Pi. This is my the onCreate function of my main layout, but it returns this error and I can't figure out why. I can publish from a command prompt so there is no problem with the broker.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bureau_klein);

    final String clientId = "Android1";
    final String ip = "192.168.1.198:1883";
    final MqttAndroidClient client = new MqttAndroidClient(this.getApplicationContext(), ip, clientId);

    try {
        IMqttToken token = client.connect();
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d("TAG", "onSuccess");
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d("TAG", "onFailure "+ clientId + " " + ip);
                Log.e("MYAPP", "exception", exception);
                Log.e("MYAPP", "exception", asyncActionToken.getException());
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

Error:

06-14 20:24:36.825 5891-5891/com.david.domotica2 E/MYAPP: exception
java.lang.IllegalArgumentException: 192.168.1.198:1883
  at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:473)
  at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
  at org.eclipse.paho.android.service.MqttConnection.connect(MqttConnection.java:282)
  at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:323)
  at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:462)
  at org.eclipse.paho.android.service.MqttAndroidClient.access$200(MqttAndroidClient.java:70)
  at org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection.onServiceConnected(MqttAndroidClient.java:109)
  at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1685)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1714)
  at android.os.Handler.handleCallback(Handler.java:789)
  at android.os.Handler.dispatchMessage(Handler.java:98)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6798)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
06-14 20:24:36.825 5891-5891/com.david.domotica2 E/MYAPP: exception

Solution

  • What you are passing to the MqttAndroidClient as an IP address should be a full URI.

    e.g. you are passing in 192.168.1.198:1883 which is a IP address and a port but is missing a schema (protocol)

    In this case you should be passing in tcp://192.168.1.198:1883