Skip to content Skip to sidebar Skip to footer

Paho-mqtt Callbacks In Different Classes Android Java

I've just started Java Android programming, or even Java programming in general and I wanted to implement the Paho MQTT Android Service using a certain MqttHandler class and I want

Solution 1:

The proper way to set callback handler is setting it in MQTT client, like following:

publicclassMTTConnector { 
            publicvoid connect {
              MqttAndroidClient mqttClient = newMqttAndroidClient(BaseApplication.getAppContext(), broker, MQTT_CLIENT_ID); 
              mqttClient.setCallback(newMqttCallbackHandler(BaseApplication.getAppContext()));
          }
        }

    publicclassMqttCallbackHandlerimplementsMqttCallbackExtended {
    @OverridepublicvoidconnectComplete(boolean b, String s) {
            Log.w("mqtt", s);
        }

        @OverridepublicvoidconnectionLost(Throwable throwable) {

        }

        @OverridepublicvoidmessageArrived(String topic, MqttMessage mqttMessage) throws Exception {
            Log.w("Anjing", mqttMessage.toString());
        }

        @OverridepublicvoiddeliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        }
  }

If you are using latest version your call back need to implement MqttCallbackExtended class, see following link for more details on online GitHub project.

MqttCallbackExtended.java

Inside your activity initialize MTTConnector and call connect.

Hoping this help.

Cheers !

Post a Comment for "Paho-mqtt Callbacks In Different Classes Android Java"