1

I'm trying to write an application in android to publish mqtt messages. I'm using an AsyncTask to publish to my broker but sometimes I have a big delay I guess is because I have to connect to the broker every time before publishing

@Override
protected Void doInBackground(String... params) {

    try {
        settings = new AppSettings(context);

        if (!settings.getBroker().isEmpty()) {

            client = new MqttClient(settings.getBroker(), MQTTService.ANDROID_ID, new MemoryPersistence());

            if (!settings.getUsername().isEmpty() || settings.getPassword().isEmpty()) {
                MqttConnectOptions options = new MqttConnectOptions();
                options.setUserName(settings.getUsername());
                options.setPassword(settings.getPassword().toCharArray());
                client.setCallback(new MQTTPushCallback(context));
                client.connect(options);
            } else {
                client.setCallback(new MQTTPushCallback(context));
                client.connect();
            }

            if (client.isConnected()) {
                client.publish(settings.getTopic(), new MqttMessage(params[0].getBytes()));
            } else {
                Log.d(TAG, "Client is not connected to the mqtt service");
            }

        } else {
            Log.d(TAG, "Broker URL unavailable !");
        }

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

    return null;
}

My question is , is there a better way of doing this , maybe keeping the connection alive maybe in a service ?

Best regards,
Paul.

Paul Dumitru
  • 68
  • 1
  • 10

2 Answers2

0

There is a Android service on the paho site (https://eclipse.org/paho/clients/android/) you really should be using this to keep a connection open and live and then just publishing data

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • I know but there is no documentation on how to use that .. that's why I'm only using the jar – Paul Dumitru May 21 '15 at 16:20
  • There is sample code linked to from the end of the page I gave the URL for (https://eclipse.org/paho/clients/android/sample/) – hardillb May 21 '15 at 16:23
  • Yes I know how to read , also I've cloned the git repository , the nexus repository says "Resource not found" I'm trying to figure it out for more than a couple days now . I have written a service and there is no problem with receiving (subscribing) , my problem is with publishing ... – Paul Dumitru May 21 '15 at 16:26
0

OK, so it turns out it's pretty straightforward, what I've done was to create a sub class and extend the BroadcastReceiver

public class PublishBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG , "onReceive");
        if((client != null) && (client.isConnected())){
            String Payload = intent.getStringExtra(EXTRA_MESSAGE);
            MqttMessage message = new MqttMessage(Payload.getBytes());
            try {
                client.publish(TOPIC ,message);
            } catch (MqttException e) {
                e.printStackTrace();
            }
        }
    }
}

Than at onCreate() in my service I've registered the receiver

LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new PublishBroadcast() ,
            new IntentFilter(ACTION_PUBLISH));
Paul Dumitru
  • 68
  • 1
  • 10