Exemple #1
0
    @Override
    protected Object doInBackground(Object... params) {

      log("create connection");
      String mqttConnSpec = "tcp://" + brokerHostName + "@" + MQTT_BROKER_PORT_NUM;
      // Create the client and connect
      try {
        mqttClient = MqttClient.createMqttClient(mqttConnSpec, MQTT_PERSISTENCE);

        String clientID = MQTT_CLIENT_ID + "/" + mPrefs.getString(PREF_DEVICE_ID, "");

        mqttClient.connect(clientID, MQTT_CLEAN_START, MQTT_KEEP_ALIVE);
        // register this client app has being able to receive messages
        mqttClient.registerSimpleHandler(this);
        // Subscribe to an initial topic, which is combination of client
        // ID
        // and device ID.
        initTopic = MQTT_CLIENT_ID + "/" + initTopic;
        subscribeToTopic(initTopic);
      } catch (MqttException e) {
        e.printStackTrace();
      }
      log("Connection established to " + brokerHostName + " on topic " + initTopic);

      // Save start time
      mStartTime = System.currentTimeMillis();
      // Star the keep-alives
      startKeepAlives();
      return null;
    }