mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId); mqttAndroidClient.setCallback(new MqttCallbackExtended() { @Override public void connectComplete(boolean reconnect, String serverURI) { mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) {
/** * Subscribe to a topic, which may include wildcards. * * @see #subscribe(String[], int[], Object, IMqttActionListener) * * @param topicFilter the topic to subscribe to, which can include wildcards. * @param qos the maximum quality of service at which to subscribe. Messages * published at a lower quality of service will be received at the published * QoS. Messages published at a higher quality of service will be received using * the QoS specified on the subscribe. * @param messageListener a callback to handle incoming messages * @return token used to track and wait for the subscribe to complete. The token * will be passed to callback methods if set. * @throws MqttException if there was an error registering the subscription. */ public IMqttToken subscribe(String topicFilter, int qos, IMqttMessageListener messageListener) throws MqttException { return subscribe(topicFilter, qos, null, null, messageListener); }
/** * Publishes a message to a topic on the server. Takes an * {@link MqttMessage} message and delivers it to the server at the * requested quality of service. * * @param topic * to deliver the message to, for example "finance/stock/ibm". * @param message * to deliver to the server * @return token used to track and wait for the publish to complete. The * token will be passed to any callback that has been set. * @throws MqttPersistenceException * when a problem occurs storing the message * @throws IllegalArgumentException * if value of QoS is not 0, 1 or 2. * @throws MqttException * for other errors encountered while publishing the message. * For instance client not connected. * @see #publish(String, MqttMessage, Object, IMqttActionListener) */ @Override public IMqttDeliveryToken publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException { return publish(topic, message, null, null); }
public void publishMessage(){ try { MqttMessage message = new MqttMessage(); message.setPayload(publishMessage.getBytes()); mqttAndroidClient.publish(publishTopic, message); addToHistory("Message Published"); if(!mqttAndroidClient.isConnected()){ addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer."); } } catch (MqttException e) { System.err.println("Error Publishing: " + e.getMessage()); e.printStackTrace(); } }
public void connect(Connection connection) { String[] actionArgs = new String[1]; actionArgs[0] = connection.getId(); final ActionListener callback = new ActionListener(this, ActionListener.Action.CONNECT, connection, actionArgs); connection.getClient().setCallback(new MqttCallbackHandler(this, connection.handle())); try { connection.getClient().connect(connection.getConnectionOptions(), null, callback); } catch (MqttException e) { Log.e(this.getClass().getCanonicalName(), "MqttException occurred", e); } }
mqttClient = new MqttAndroidClient(mContext, mqttSSLServerURI, "testSSLPubSub"); options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"), keyStorePwd)); mqttClient.setCallback(mqttV3Receiver); connectToken = mqttClient.connect(options); connectToken.waitForCompletion(waitForCompletionTime); byte[] message = mqttMessage.getPayload(); subToken = mqttClient.subscribe(topicNames, topicQos, null, null); subToken.waitForCompletion(waitForCompletionTime); pubToken = mqttClient.publish(topicNames[0], message, 0, false, null, null); pubToken.waitForCompletion(waitForCompletionTime); disconnectToken = mqttClient.disconnect(null, null); disconnectToken.waitForCompletion(waitForCompletionTime); mqttClient.close();
mClient.setCallback(mClientCb); mClient.connect(options, mContext, mConnectionCb); } catch (MqttException e) { e.printStackTrace(); mClient.disconnect(mContext, mConnectionCb); return mClient.publish(topic, mqttMessage, mContext, listener); } catch (MqttPersistenceException e) { e.printStackTrace(); mClient.subscribe(topic, 0, mContext, listener); } catch (MqttException e) { e.printStackTrace(); mClient.unsubscribe(topic, mContext, listener); } catch (MqttException e) { e.printStackTrace();
mqttClient = new MqttAndroidClient(mContext, mqttSSLServerURI, "testSSLConnect"); options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"), keyStorePwd)); connectToken = mqttClient.connect( options); connectToken.waitForCompletion(waitForCompletionTime); disconnectToken = mqttClient.disconnect(null, null); disconnectToken.waitForCompletion(waitForCompletionTime); connectToken = mqttClient.connect(options); connectToken.waitForCompletion(waitForCompletionTime); disconnectToken = mqttClient.disconnect(null, null); disconnectToken.waitForCompletion(waitForCompletionTime); mqttClient.close();
client.connect(null, new IMqttActionListener() { client.publish("messages", message); client.disconnect(); Log.i(LOGTAG, "client disconnected"); } catch (MqttPersistenceException e) {
connection.getClient().disconnect(); final ActionListener callback = new ActionListener(this, ActionListener.Action.CONNECT, connection, actionArgs); connection.getClient().setCallback(new MqttCallbackHandler(this, model.getClientHandle())); connection.getClient().setTraceCallback(new MqttTraceCallback()); MqttConnectOptions connOpts = optionsFromModel(model); connection.addConnectionOptions(connOpts); drawerFragment.updateConnection(connection); connection.getClient().connect(connOpts, null, callback); Fragment fragment = new ConnectionFragment(); Bundle bundle = new Bundle();
public MqttHelper(Context context){ mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId); mqttAndroidClient.setCallback(new MqttCallbackExtended() { @Override public void connectComplete(boolean b, String s) { Log.w("mqtt", s); } @Override public void connectionLost(Throwable throwable) { } @Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { Log.w("Mqtt", mqttMessage.toString()); } @Override public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { } }); connect(); }
public void updateConnection(String clientId, String host, int port, boolean tlsConnection){ String uri; if(tlsConnection) { uri = "ssl://" + host + ":" + port; } else { uri = "tcp://" + host + ":" + port; } this.clientId = clientId; this.host = host; this.port = port; this.tlsConnection = tlsConnection; this.client = new MqttAndroidClient(context, uri, clientId); }
/** * Connects to an MQTT server using the default options. * <p> * The default options are specified in {@link MqttConnectOptions} class. * </p> * * @throws MqttException * for any connected problems * @return token used to track and wait for the connect to complete. The * token will be passed to the callback methods if a callback is * set. * @see #connect(MqttConnectOptions, Object, IMqttActionListener) */ @Override public IMqttToken connect() throws MqttException { return connect(null, null); }
final ActionListener callback = new ActionListener(this, ActionListener.Action.CONNECT, connection, actionArgs); connection.getClient().setCallback(new MqttCallbackHandler(this, model.getClientHandle())); connection.getClient().setTraceCallback(new MqttTraceCallback()); connection.getClient().connect(connOpts, null, callback); Fragment fragment = new ConnectionFragment(); Bundle bundle = new Bundle();
clientConnectionListener.setCallback(callback); mMqttAndroidClient.setCallback(clientConnectionListener); Log.v(TAG, "Subscription Infrastructure: Calling MQTT Connect with actual endpoint for client ID[" + mMqttAndroidClient.getClientId() + "]"); mMqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { Log.e("TAG", "Subscription Infrastructure: Failed to connect mqtt client for clientID [" + mMqttAndroidClient.getClientId() + "]" , e); callback.onError(e);
public MqttAndroidClient getMqttClient(Context context, String brokerUrl, String clientId) { mqttAndroidClient = new MqttAndroidClient(context, brokerUrl, clientId); try { IMqttToken token = mqttAndroidClient.connect(getMqttConnectionOption()); token.setActionCallback(new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { mqttAndroidClient.setBufferOpts(getDisconnectedBufferOptions()); Log.d(TAG, "Success"); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.d(TAG, "Failure " + exception.toString()); } }); } catch (MqttException e) { e.printStackTrace(); } return mqttAndroidClient; }
/** * Setter for MQTT callbacks. * @param callback MQTTCallBack to receive messages and delivery complete statuses. */ public void setCallback(MqttCallbackExtended callback) { mqttAndroidClient.setCallback(callback); }
public void disconnect(Connection connection){ try { connection.getClient().disconnect(); } catch( MqttException ex){ Log.e(TAG, "Exception occurred during disconnect: " + ex.getMessage()); } }
/** * Requests the server to unsubscribe the client from one or more topics. * * @param topic * one or more topics to unsubscribe from. Each topic must match * one specified on an earlier subscription. * @return token used to track and wait for the unsubscribe to complete. The * token will be passed to callback methods if set. * @throws MqttException * if there was an error unregistering the subscription. * * @see #unsubscribe(String[], Object, IMqttActionListener) */ @Override public IMqttToken unsubscribe(String[] topic) throws MqttException { return unsubscribe(topic, null, null); }
/** * @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())); } }