I'm using paho to subscribe to the mqtt topic "$SYS/#", in order to see client's connections.
I implemented the MqttCallback interface to receive message send in this topic, and for the moment I just want to display the content of message in the console.
My problem is that the message is received endlessly, the "messageArrived" function is call each second with always the same client name.
import android.app.Activity;
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.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MainActivity extends Activity {
private static final String MQTT_DIR = "/home/persistence.log";
private static final String MQTT_URI = "tcp://x.x.x.x:1883";
private MqttClient client;
private String clientId = "AndroidClient";
private String clientName = "usermqtt";
private char[] password = {'x', 'x', 'x'};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MemoryPersistence persistence = new MemoryPersistence();
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(clientName);
options.setPassword(password);
try {
client = new MqttClient(MQTT_URI, clientId, persistence);
client.connect(options);
client.subscribe("$SYS/#");
} catch (MqttException e) {
e.printStackTrace();
}
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String arg0, MqttMessage arg1)
throws Exception {
// TODO Auto-generated method stub
System.out.println("Message Received");
System.out.println(arg0);
System.out.println(arg1.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}