The callBacklistener is called Twice?

2019-06-05 01:00发布

In the below, when i run it and intentionally turn the WiFi off, the callBack clientCallBack.connectionLost displays the message Log.d(TAG, "@connectionLost: MQTT Server connection lost"); twice. And When I re connect, the message Log.i(TAG, "@onSuccess: Connection Successful."); in the client_1.connect callback is displayed only once.

anyone can expain why i receive the message from the calback twice?

Code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mqtt_proj_01_layout);

    sdCard = Environment.getExternalStorageDirectory();
    folder = new File(sdCard + directory);
    if (!folder.exists())
        folder.mkdir();

    final MqttClientPersistence persistenceDataDir = new MqttDefaultFilePersistence(folder.toString());

    final MqttAndroidClient client_1 = new MqttAndroidClient(getApplicationContext(), serverURI, clientID, persistenceDataDir, Ack.AUTO_ACK);

    MqttConnectOptions opts = new MqttConnectOptions();
    opts.setCleanSession(false);
    opts.setWill(WILL_TOPIC, WILL_MSG.getBytes(), 1, true);
    opts.setKeepAliveInterval(keepAliveInterval);

    if (client_1 != null) {
        //bind the callback to the client object before the client connect o the server so that you get notified regarding any 
        //pending notifications.this is called Asynchronous massaging
        client_1.setCallback(new clientCallBack());

        try {
            /**
             * For synchronous Client " events that happens synchronously, in which the call will lock and return either 
             * onSuccess or onFailure.
             */
            client_1.connect(opts, getApplicationContext(), new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken arg0) {
                // TODO Auto-generated method stub
                Log.i(TAG, "@onSuccess: Connection Successful.");
                try {
                    client_1.subscribe(TOPIC, 1);
                } catch (MqttSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IMqttToken arg0, Throwable arg1) {
            // TODO Auto-generated method stub
                Log.i(TAG, "@onFailure: Connection Failed.");
                }
            });
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e(TAG, "Connection problems.");
            }
        }
}

public class clientCallBack implements MqttCallback {

    @Override
    public void connectionLost(Throwable e) {
        // TODO Auto-generated method stub
        Log.d(TAG, "@connectionLost: MQTT Server connection lost");
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken arg0) {
        // TODO Auto-generated method stub
        Log.d(TAG, "@deliveryComplete: sDelivery Completed");
    }

    @Override
    public void messageArrived(String topic, MqttMessage message)
            throws Exception {
        // TODO Auto-generated method stub
        Log.d(TAG, "@messageArrived: Message Arrived");

        Log.d(TAG, "**********Topic: "+topic+"**********");
        Log.d(TAG, "Message: "+message.toString());
        Log.d(TAG, "QoS: "+message.getQos());
        Log.d(TAG, "payload: "+message.getPayload());
        Log.d(TAG, "isDuplicate? "+message.isDuplicate());
        Log.d(TAG, "isRetained?"+message.isRetained());

    }

}

0条回答
登录 后发表回答