Exemple #1
0
 /*
  * Sends a message to the message broker, requesting that it be
  * published to the specified topic.
  */
 private void publishToTopic(String topicName, String message) throws MqttException {
   if ((mqttClient == null) || (mqttClient.isConnected() == false)) {
     // quick sanity check - don't try and publish if we don't have
     // a connection
     log("No connection to public to");
   } else {
     mqttClient.publish(
         topicName, message.getBytes(), MQTT_QUALITY_OF_SERVICE, MQTT_RETAINED_PUBLISH);
   }
 }
Exemple #2
0
    /*
     * Send a request to the message broker to be sent messages published
     * with the specified topic name. Wildcards are allowed.
     */
    private void subscribeToTopic(String topicName) throws MqttException {

      if ((mqttClient == null) || (mqttClient.isConnected() == false)) {
        // quick sanity check - don't try and subscribe if we don't have
        // a connection
        log("Connection error" + "No connection");
      } else {
        String[] topics = {topicName};
        mqttClient.subscribe(topics, MQTT_QUALITIES_OF_SERVICE);
      }
    }