/** * Acknowledges a message received on the * {@link MqttCallback#messageArrived(String, MqttMessage)} * * @param messageId * the messageId received from the MqttMessage (To access this * field you need to cast {@link MqttMessage} to * {@link ParcelableMqttMessage}) * @return whether or not the message was successfully acknowledged */ public boolean acknowledgeMessage(String messageId) { if (messageAck == Ack.MANUAL_ACK) { Status status = mqttService.acknowledgeMessageArrival(clientHandle, messageId); return status == Status.OK; } return false; }
public void subscribe(String[] topicFilters, int[] qos, String invocationContext, String activityToken, IMqttMessageListener[] messageListeners) { service.traceDebug(TAG, "subscribe({" + Arrays.toString(topicFilters) + "}," + Arrays.toString(qos) + ",{" + invocationContext + "}, {" + activityToken + "}"); final Bundle resultBundle = new Bundle(); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken); resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext); if((myClient != null) && (myClient.isConnected())){ IMqttActionListener listener = new MqttConnectionListener(resultBundle); try { myClient.subscribe(topicFilters, qos,messageListeners); } catch (Exception e){ handleException(resultBundle, e); } } else { resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, NOT_CONNECTED); service.traceError("subscribe", NOT_CONNECTED); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); } }
/** * Close the client. Releases all resource associated with the client. After * the client has been closed it cannot be reused. For instance attempts to * connect will fail. * */ @Override public void close() { if(mqttService != null){ if (clientHandle == null) { clientHandle = mqttService.getClient(serverURI, clientId, myContext.getApplicationInfo().packageName,persistence); } mqttService.close(clientHandle); } }
/** * Disconnect from the server * * @param clientHandle * identifies the MqttConnection to use * @param invocationContext * arbitrary data to be passed back to the application * @param activityToken * arbitrary identifier to be passed back to the Activity */ public void disconnect(String clientHandle, String invocationContext, String activityToken) { MqttConnection client = getConnection(clientHandle); client.disconnect(invocationContext, activityToken); connections.remove(clientHandle); // the activity has finished using us, so we can stop the service // the activities are bound with BIND_AUTO_CREATE, so the service will // remain around until the last activity disconnects stopSelf(); }
/** * Actually do the mqtt connect operation */ private void doConnect() { if (clientHandle == null) { clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName, persistence); } mqttService.setTraceEnabled(traceEnabled); mqttService.setTraceCallbackId(clientHandle); String activityToken = storeToken(connectToken); try { mqttService.connect(clientHandle, connectOptions, null, activityToken); } catch (MqttException e) { IMqttActionListener listener = connectToken.getActionCallback(); if (listener != null) { listener.onFailure(connectToken, e); } } }
service.traceDebug(TAG, "Connecting {" + serverURI + "} as {" + clientId + "}"); final Bundle resultBundle = new Bundle(); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, File myDir = service.getExternalFilesDir(TAG); myDir = service.getDir(TAG, Context.MODE_PRIVATE); resultBundle.putSerializable( MqttServiceConstants.CALLBACK_EXCEPTION, new MqttPersistenceException()); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); return; service.traceDebug(TAG, "myClient != null and the client is connecting. Connect return directly."); service.traceDebug(TAG,"Connect return:isConnecting:"+isConnecting+".disconnected:"+disconnected); }else if(!disconnected){ service.traceDebug(TAG,"myClient != null and the client is connected and notify!"); doAfterConnectSuccess(resultBundle); service.traceDebug(TAG, "myClient != null and the client is not connected"); service.traceDebug(TAG,"Do Real connect!"); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener); service.traceDebug(TAG,"Do Real connect!"); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener);
/** * Get the status of a specific client * * @param clientHandle * identifies the MqttConnection to use * @return true if the specified client is connected to an MQTT server */ public boolean isConnected(String clientHandle) { MqttConnection client = getConnection(clientHandle); return client.isConnected(); }
@Override public void onSuccess(IMqttToken asyncActionToken) { service.callbackToActivity(clientHandle, Status.OK, resultBundle); }
public void deliveryComplete(IMqttDeliveryToken messageToken) { service.traceDebug(TAG, "deliveryComplete(" + messageToken + ")"); invocationContext); service.callbackToActivity(clientHandle, Status.OK, resultBundle); service.callbackToActivity(clientHandle, Status.OK, resultBundle);
/** * Disconnects from the server. * <p> * An attempt is made to quiesce the client allowing outstanding work to * complete before disconnecting. It will wait for a maximum of 30 seconds * for work to quiesce before disconnecting. This method must not be called * from inside {@link MqttCallback} methods. * </p> * * @return token used to track and wait for disconnect to complete. The * token will be passed to any callback that has been set. * @throws MqttException * for problems encountered while disconnecting * @see #disconnect(long, Object, IMqttActionListener) */ @Override public IMqttToken disconnect() throws MqttException { IMqttToken token = new MqttTokenAndroid(this, null, null); String activityToken = storeToken(token); mqttService.disconnect(clientHandle, null, activityToken); return token; }
resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, NOT_CONNECTED); service.traceError(MqttServiceConstants.SEND_ACTION, NOT_CONNECTED); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
public int getBufferedMessageCount(){ return mqttService.getBufferedMessageCount(clientHandle); }
public MqttMessage getBufferedMessage(int bufferIndex){ return mqttService.getBufferedMessage(clientHandle, bufferIndex); }
public void deleteBufferedMessage(int bufferIndex){ mqttService.deleteBufferedMessage(clientHandle, bufferIndex); }
/** * Actually do the mqtt connect operation */ private void doConnect() { if (clientHandle == null) { clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName, persistence); } mqttService.setTraceEnabled(traceEnabled); mqttService.setTraceCallbackId(clientHandle); String activityToken = storeToken(connectToken); try { mqttService.connect(clientHandle, connectOptions, null, activityToken); } catch (MqttException e) { IMqttActionListener listener = connectToken.getActionCallback(); if (listener != null) { listener.onFailure(connectToken, e); } } }
/** * Get tokens for all outstanding deliveries for a client * * @param clientHandle * identifies the MqttConnection * @return an array (possibly empty) of tokens */ public IMqttDeliveryToken[] getPendingDeliveryTokens(String clientHandle) { MqttConnection client = getConnection(clientHandle); return client.getPendingDeliveryTokens(); }
private void handleException(final Bundle resultBundle, Exception e) { resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, e.getLocalizedMessage()); resultBundle.putSerializable(MqttServiceConstants.CALLBACK_EXCEPTION, e); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); }
/** * Callback when a message is received * * @param topic * the topic on which the message was received * @param message * the message itself */ @Override public void messageArrived(String topic, MqttMessage message) throws Exception { service.traceDebug(TAG, "messageArrived(" + topic + ",{" + message.toString() + "})"); String messageId = service.messageStore.storeArrived(clientHandle, topic, message); Bundle resultBundle = messageToBundle(messageId, topic, message); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.MESSAGE_ARRIVED_ACTION); resultBundle.putString(MqttServiceConstants.CALLBACK_MESSAGE_ID, messageId); service.callbackToActivity(clientHandle, Status.OK, resultBundle); }
/** * Disconnects from the server. * <p> * An attempt is made to quiesce the client allowing outstanding work to * complete before disconnecting. It will wait for a maximum of the * specified quiesce time for work to complete before disconnecting. This * method must not be called from inside {@link MqttCallback} methods. * </p> * * @param quiesceTimeout * the amount of time in milliseconds to allow for existing work * to finish before disconnecting. A value of zero or less means * the client will not quiesce. * @return token used to track and wait for disconnect to complete. The * token will be passed to the callback methods if a callback is * set. * @throws MqttException * for problems encountered while disconnecting * @see #disconnect(long, Object, IMqttActionListener) */ @Override public IMqttToken disconnect(long quiesceTimeout) throws MqttException { IMqttToken token = new MqttTokenAndroid(this, null, null); String activityToken = storeToken(token); mqttService.disconnect(clientHandle, quiesceTimeout, null, activityToken); return token; }
/** * Disconnect from the server * * @param clientHandle * identifies the MqttConnection to use * @param quiesceTimeout * in milliseconds * @param invocationContext * arbitrary data to be passed back to the application * @param activityToken * arbitrary identifier to be passed back to the Activity */ public void disconnect(String clientHandle, long quiesceTimeout, String invocationContext, String activityToken) { MqttConnection client = getConnection(clientHandle); client.disconnect(quiesceTimeout, invocationContext, activityToken); connections.remove(clientHandle); // the activity has finished using us, so we can stop the service // the activities are bound with BIND_AUTO_CREATE, so the service will // remain around until the last activity disconnects stopSelf(); }