1
votes

I'm using the Paho Android Client. https://eclipse.org/paho/clients/java/

This is probably THE go to library for MQTT on Android, yet it doesn't support secure MQTT websockets (wss://), giving me IllegalArgument exceptions for the server uri.

I have been looking for a solution to connect to my MQTT websocket which has a wss:// path scheme, and so far there's only one library: https://github.com/inventit/mqtt-websocket-java

Which also doesn't work! I'm getting Jetty SSL exceptions.

If you have an implementation you've used before, please share them with me, this has taken a lot of my time, and I'm still clueless, thanks!

For both libraries, I've tried using the sample code they offer in their documentations.

2
There is no real question here - hardillb
@hardillb The question is how to connect to an MQTT websocket on android, as the current solutions don't work? - Illinois47

2 Answers

0
votes

I think Paho Android Client doesn't support websocket ssl but you can use MqttAsyncClient instead MqttAndroidClient like this :

The libraries are the same :

    dependencies {
    ...
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
    compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.0'
    ...
    }

Use MqttAsyncClient instead MqttAndroidClient like this :

private MqttAsyncClient mMqttAndroidClient;
try {
      mMqttAndroidClient = new MqttAsyncClient("wss://...", MqttClient.generateClientId(), new MemoryPersistence());
    } catch (MqttException e) {
      e.printStackTrace();
    }
0
votes

I used "WSS://" without any problem. Make a MqttAndroidClient object with "WSS" schema and port 443.

mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setKeepAliveInterval(MqttConfig.KEEPALIVE);
    mqttConnectOptions.setUserName("username");
    mqttConnectOptions.setPassword("pass");
    mqttConnectOptions.setCleanSession(false);
    mqttConnectOptions.setAutomaticReconnect(true);
    mqttConnectOptions.setMaxInflight(1024);
    ..
    uri="wss://broker.hivemq.com:443"
    MqttAndroidClient client = new MqttAndroidClient(context, uri, clientId,persistence);
    ..
    getClient().connect(mqttConnectOptions,applicationContext,mqttActionListener;