/** * 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); }
/** * Subscribe to multiple topics, each of which may include wildcards. * * <p>Provides an optimized way to subscribe to multiple topics compared to * subscribing to each one individually.</p> * * @see #subscribe(String[], int[], Object, IMqttActionListener) * * @param topicFilters one or more topics 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 messageListeners an array of callbacks 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[] topicFilters, int[] qos, IMqttMessageListener[] messageListeners) throws MqttException { return subscribe(topicFilters, qos, null, null, messageListeners); }
/** * 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 userContext optional object used to pass context to the callback. Use * null if not required. * @param callback optional listener that will be notified when subscribe * has completed * @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, Object userContext, IMqttActionListener callback, IMqttMessageListener messageListener) throws MqttException { return subscribe(new String[] {topicFilter}, new int[] {qos}, userContext, callback, new IMqttMessageListener[] {messageListener}); }
/** * Subscribe to a topic, which may include wildcards. * * @param topic * 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 * subscription. * @return token used to track and wait for the subscribe to complete. The * token will be passed to callback methods if set. * @throws MqttSecurityException * for security related problems * @throws MqttException * for non security related problems * * @see #subscribe(String[], int[], Object, IMqttActionListener) */ @Override public IMqttToken subscribe(String topic, int qos) throws MqttException, MqttSecurityException { return subscribe(topic, qos, null, null); }
public IMqttToken subscribe(String[] topic, int[] qos) throws MqttException, MqttSecurityException { return subscribe(topic, qos, null, null);
public void subscribeToTopic(){ try { mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { addToHistory("Subscribed!"); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { addToHistory("Failed to subscribe"); } }); // THIS DOES NOT WORK! mqttAndroidClient.subscribe(subscriptionTopic, 0, new IMqttMessageListener() { @Override public void messageArrived(String topic, MqttMessage message) throws Exception { // message Arrived! System.out.println("Message: " + topic + " : " + new String(message.getPayload())); } }); } catch (MqttException ex){ System.err.println("Exception whilst subscribing"); ex.printStackTrace(); } }
/** * A connection action has been successfully completed, update the * connection object associated with the client this action belongs to and * then notify the user of success. */ private void connect() { Connection c = Connections.getInstance(context).getConnection(clientHandle); c.changeConnectionStatus(Connection.ConnectionStatus.CONNECTED); c.addAction("Client Connected"); Log.i(TAG, c.handle() + " connected."); try { ArrayList<Subscription> subscriptions = connection.getSubscriptions(); for (Subscription sub : subscriptions) { Log.i(TAG, "Auto-subscribing to: " + sub.getTopic() + "@ QoS: " + sub.getQos()); connection.getClient().subscribe(sub.getTopic(), sub.getQos()); } } catch (MqttException ex){ Log.e(TAG, "Failed to Auto-Subscribe: " + ex.getMessage()); } }
public void addNewSubscription(Subscription subscription) throws MqttException { if(!subscriptions.containsKey(subscription.getTopic())){ try{ String[] actionArgs = new String[1]; actionArgs[0] = subscription.getTopic(); final ActionListener callback = new ActionListener(this.context, ActionListener.Action.SUBSCRIBE, this, actionArgs); this.getClient().subscribe(subscription.getTopic(), subscription.getQos(), null, callback); Persistence persistence = new Persistence(context); long rowId = persistence.persistSubscription(subscription); subscription.setPersistenceId(rowId); subscriptions.put(subscription.getTopic(), subscription); } catch (PersistenceException pe){ throw new MqttException(pe); } } }
/** * 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); }
/** * 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); }
/** * Subscribe to multiple topics, each of which may include wildcards. * * <p>Provides an optimized way to subscribe to multiple topics compared to * subscribing to each one individually.</p> * * @see #subscribe(String[], int[], Object, IMqttActionListener) * * @param topicFilters one or more topics 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 messageListeners an array of callbacks 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[] topicFilters, int[] qos, IMqttMessageListener[] messageListeners) throws MqttException { return subscribe(topicFilters, qos, null, null, messageListeners); }
public void subscribe(String topic, int qos) { if (mMqttClient != null && mMqqtClientStatus == MqqtConnectionStatus.CONNECTED && topic != null) { try { Log.d(TAG, "Mqtt: subscribe to " + topic + " qos:" + qos); mMqttClient.subscribe(topic, qos); } catch (MqttException e) { Log.e(TAG, "Mqtt:x subscribe error: ", e); } } }
/** * 订阅主题 * * @param topics 主题 * @param qos 策略 */ public void subscribe(String[] topics, int[] qos) { try { // 订阅topic话题 Log.i(TAG, "execute subscribe -- qos = " + qos.toString()); client.subscribe(topics, qos); } catch (Exception e) { Log.e(TAG,e.toString()); } }
public void subscribe(@NonNull MqttAndroidClient client, @NonNull final String topic, int qos) throws MqttException { IMqttToken token = client.subscribe(topic, qos); token.setActionCallback(new IMqttActionListener() { @Override public void onSuccess(IMqttToken iMqttToken) { Log.d(TAG, "Subscribe Successfully " + topic); } @Override public void onFailure(IMqttToken iMqttToken, Throwable throwable) { Log.e(TAG, "Subscribe Failed " + topic); } }); }
byte[] message = mqttMessage.getPayload(); subToken = mqttClient.subscribe(topicNames, topicQos, null, new ActionListener(notifier)); subToken.waitForCompletion(waitForCompletionTime);
private void subscribeToTopic() { try { mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { Log.w("Mqtt","Subscribed!"); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w("Mqtt", "Subscribed fail!"); } }); } catch (MqttException ex) { System.err.println("Exception whilst subscribing"); ex.printStackTrace(); } } }
byte[] message = mqttMessage.getPayload(); subToken = mqttClient.subscribe(topicNames, topicQos, null, null); subToken.waitForCompletion(waitForCompletionTime);
/** * Used for subscribing to the topic. */ private void subscribeToTopic() { try { mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { Log.w(TAG, "Subscribed!"); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { Log.w(TAG, "Subscribed fail!"); } }); } catch (MqttException ex) { Log.e(TAG, "Exception whilst subscribing"); ex.printStackTrace(); } } }
public void subscribeToTopic(final BrokerEntity brokerEntity, String topic, int qos){ MqttAndroidClient mqttAndroidClient = clients.get(brokerEntity.getId()); if(mqttAndroidClient!=null){ try { mqttAndroidClient.subscribe(topic, qos); } catch (MqttException e) { e.printStackTrace(); } } }
@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); } }