I'm using MQTT with paho to receive and publish messages on android.
I have the following code for my MQTT initialization.
private void initializeMQTT(){
try{
mqttClient = new MqttClient(
"tcp://broker.hivemq.com:1883",
MqttClient.generateClientId(),
new MemoryPersistence()
);
mqttClient.connect();
mqttConnected = mqttClient.isConnected();
mqttClient.subscribe("testtopic/listen",1);
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) { //Called when the client lost the connection to the broker
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
mqttPayload = topic + ": " + Arrays.toString(message.getPayload());
mqttAnswer.setPayload(mqttPayload.getBytes());
mqttClient.publish("testtopic/publish",mqttAnswer);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {//Called when a outgoing publish is complete
messageInfoTest = "message was sent";
}
});
}
catch(MqttException e){
}
}
I just want to take the message I receive and send it back somewhere else as a test.
What happens right now is I receive the first time I publish on the testtopic/receive topic. I do not seem to be publishing anything back. And if I try to send another message to testtopic/receive it is never receive on my android.
Anybody has an idea of what I'm currently missing?
Thanks!