/** * Get an MqttConnection object to represent a connection to a server * * @param serverURI specifies the protocol, host name and port to be used to connect to an MQTT server * @param clientId specifies the name by which this connection should be identified to the server * @param contextId specifies the app conext info to make a difference between apps * @param persistence specifies the persistence layer to be used with this client * @return a string to be used by the Activity as a "handle" for this * MqttConnection */ public String getClient(String serverURI, String clientId, String contextId, MqttClientPersistence persistence) { String clientHandle = serverURI + ":" + clientId+":"+contextId; if (!connections.containsKey(clientHandle)) { MqttConnection client = new MqttConnection(this, serverURI, clientId, persistence, clientHandle); connections.put(clientHandle, client); } return clientHandle; }
private void doAfterConnectSuccess(final Bundle resultBundle) { //since the device's cpu can go to sleep, acquire a wakelock and drop it later. acquireWakeLock(); service.callbackToActivity(clientHandle, Status.OK, resultBundle); deliverBacklog(); setConnectingState(false); disconnected = false; releaseWakeLock(); }
/** * Close connection from a particular client * * @param clientHandle * identifies the MqttConnection to use */ public void close(String clientHandle) { MqttConnection client = getConnection(clientHandle); client.close(); }
private void doAfterConnectFail(final Bundle resultBundle){ // acquireWakeLock(); disconnected = true; setConnectingState(false); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); releaseWakeLock(); }
}else if(!disconnected){ service.traceDebug(TAG,"myClient != null and the client is connected and notify!"); doAfterConnectSuccess(resultBundle); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener); setConnectingState(false); handleException(resultBundle, e);
/** * @see android.app.Service#onDestroy() */ @Override public void onDestroy() { // disconnect immediately for (MqttConnection client : connections.values()) { client.disconnect(null, null); } // clear down if (mqttServiceBinder != null) { mqttServiceBinder = null; } unregisterBroadcastReceivers(); if (this.messageStore !=null ) this.messageStore.close(); super.onDestroy(); }
/** * Receive notification that we are offline<br> * if cleanSession is true, we need to regard this as a disconnection */ void offline() { if (!disconnected && !cleanSession) { Exception e = new Exception("Android offline"); connectionLost(e); } }
public MqttMessage getBufferedMessage(String clientHandle, int bufferIndex){ MqttConnection client = getConnection(clientHandle); return client.getBufferedMessage(bufferIndex); }
public void deleteBufferedMessage(String clientHandle, int bufferIndex){ MqttConnection client = getConnection(clientHandle); client.deleteBufferedMessage(bufferIndex); }
/** * Connect to the MQTT server specified by a particular client * * @param clientHandle * identifies the MqttConnection to use * @param connectOptions * the MQTT connection options to be used * @param invocationContext * arbitrary data to be passed back to the application * @param activityToken * arbitrary identifier to be passed back to the Activity * @throws MqttSecurityException thrown if there is a security exception * @throws MqttException thrown for all other MqttExceptions */ public void connect(String clientHandle, MqttConnectOptions connectOptions, String invocationContext, String activityToken) throws MqttSecurityException, MqttException { MqttConnection client = getConnection(clientHandle); client.connect(connectOptions, null, activityToken); }
@Override public void connectComplete(boolean reconnect, String serverURI) { Bundle resultBundle = new Bundle(); resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.CONNECT_EXTENDED_ACTION); resultBundle.putBoolean(MqttServiceConstants.CALLBACK_RECONNECT, reconnect); resultBundle.putString(MqttServiceConstants.CALLBACK_SERVER_URI, serverURI); doAfterConnectSuccess(resultBundle); //service.callbackToActivity(clientHandle, Status.OK, resultBundle); }
private void doAfterConnectFail(final Bundle resultBundle){ // acquireWakeLock(); disconnected = true; setConnectingState(false); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); releaseWakeLock(); }
}else if(!disconnected){ service.traceDebug(TAG,"myClient != null and the client is connected and notify!"); doAfterConnectSuccess(resultBundle); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener); setConnectingState(true); myClient.connect(connectOptions, invocationContext, listener); setConnectingState(false); handleException(resultBundle, e);
/** * 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(); }
/** * Receive notification that we are offline<br> * if cleanSession is true, we need to regard this as a disconnection */ void offline() { if (!disconnected && !cleanSession) { Exception e = new Exception("Android offline"); connectionLost(e); } }
public MqttMessage getBufferedMessage(String clientHandle, int bufferIndex){ MqttConnection client = getConnection(clientHandle); return client.getBufferedMessage(bufferIndex); }
public void deleteBufferedMessage(String clientHandle, int bufferIndex){ MqttConnection client = getConnection(clientHandle); client.deleteBufferedMessage(bufferIndex); }
/** * Connect to the MQTT server specified by a particular client * * @param clientHandle * identifies the MqttConnection to use * @param connectOptions * the MQTT connection options to be used * @param invocationContext * arbitrary data to be passed back to the application * @param activityToken * arbitrary identifier to be passed back to the Activity * @throws MqttSecurityException thrown if there is a security exception * @throws MqttException thrown for all other MqttExceptions */ public void connect(String clientHandle, MqttConnectOptions connectOptions, String invocationContext, String activityToken) throws MqttSecurityException, MqttException { MqttConnection client = getConnection(clientHandle); client.connect(connectOptions, null, activityToken); }
private void doAfterConnectSuccess(final Bundle resultBundle) { //since the device's cpu can go to sleep, acquire a wakelock and drop it later. acquireWakeLock(); service.callbackToActivity(clientHandle, Status.OK, resultBundle); deliverBacklog(); setConnectingState(false); disconnected = false; releaseWakeLock(); }
private void doAfterConnectFail(final Bundle resultBundle){ // acquireWakeLock(); disconnected = true; setConnectingState(false); service.callbackToActivity(clientHandle, Status.ERROR, resultBundle); releaseWakeLock(); }