@Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w(TAG, "Subscription Infrastructure: Got exception [" + exception +"] when attempting to disconnect clientID " + mMqttAndroidClient.getClientId() + "]" ); } });
@Override public void onSuccess(IMqttToken asyncActionToken) { try { mMqttAndroidClient.close(); Log.d(TAG, "Subscription Infrastructure: Successfully closed the connection. Client ID [" + mMqttAndroidClient.getClientId() + "]"); } catch (Exception e) { Log.w(TAG, "Subscription Infrastructure: Error closing connection [" + e + "]"); } }
@Override public void close() { Log.v(TAG, "Closing MQTT client [" + mMqttAndroidClient.getClientId() + ""); try { //Disconnect the connection with quiese timeout set to 0, which means disconnect immediately. //Issue the close connection on the callback - this ensures that the connection is properly closed out before resources are freed/reclaimed by the close method. mMqttAndroidClient.disconnect(0,null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { try { mMqttAndroidClient.close(); Log.d(TAG, "Subscription Infrastructure: Successfully closed the connection. Client ID [" + mMqttAndroidClient.getClientId() + "]"); } catch (Exception e) { Log.w(TAG, "Subscription Infrastructure: Error closing connection [" + e + "]"); } } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w(TAG, "Subscription Infrastructure: Got exception [" + exception +"] when attempting to disconnect clientID " + mMqttAndroidClient.getClientId() + "]" ); } }); } catch (Exception e) { Log.w(TAG, "Got exception when closing MQTT client [" + mMqttAndroidClient.getClientId() + "]", e); } }
@Override public void subscribe(final String topic, int qos, final SubscriptionCallback callback) { try { Log.v(TAG, this + "Subscription Infrastructure: Attempting to subscribe to topic " + topic + " on clientID [" + mMqttAndroidClient.getClientId() + "]"); if (subscriptionMessageListener != null) { subscriptionMessageListener.setCallback(callback); } mMqttAndroidClient.subscribe(topic, qos, subscriptionMessageListener); topics.add(topic); } catch (Exception e) { callback.onError(topic, e); } }
if(mqttAndroidClient!=null && !TextUtils.isEmpty(mqttAndroidClient.getClientId()) && mqttAndroidClient.isConnected()){ MqttMessage mqttMessage = new MqttMessage(); mqttMessage.setId(messageId);
Log.v(TAG, "Subscription Infrastructure: Calling MQTT Connect with actual endpoint for client ID[" + mMqttAndroidClient.getClientId() + "]"); mMqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() { @Override Log.e("TAG", "Subscription Infrastructure: Failed to connect mqtt client for clientID [" + mMqttAndroidClient.getClientId() + "]" , e); callback.onError(e);
/** * @see ListActivity#onResume() */ @Override protected void onResume() { super.onResume(); arrayAdapter.notifyDataSetChanged(); //Recover connections. Map<String, Connection> connections = Connections.getInstance(this).getConnections(); //Register receivers again for (Connection connection : connections.values()){ connection.getClient().registerResources(this); connection.getClient().setCallback(new MqttCallbackHandler(this, connection.getClient().getServerURI()+connection.getClient().getClientId())); } }