Ejemplo n.º 1
0
  private void connect(Context ctx, final InitCallback callback) {

    try {
      androidClient = new MqttAndroidClient(ctx, url, deviceId, clientPersistance);
      androidClient.connect(
          opts,
          ctx,
          new IMqttActionListener() {

            @Override
            public void onFailure(IMqttToken arg0, Throwable arg1) {
              Log.i(DEBUG_TAG, "Could not connect to MQTT broker: " + arg1.getMessage());
              callback.error(new ClearBladeException(arg1.getMessage()));
            }

            @Override
            public void onSuccess(IMqttToken arg0) {
              Log.i(DEBUG_TAG, "Client Connected");
              if (isSubscribed) {
                Iterator<String> it = Message.subscribed.iterator();
                while (it.hasNext()) {
                  subscribe(it.next());
                }
              }
              callback.done(true);
            }
          });
      androidClient.setCallback(this);

    } catch (MqttException err) {
      Log.i(DEBUG_TAG, err.getLocalizedMessage());
    }
  }
Ejemplo n.º 2
0
  public void unsubscribe(String topic) {

    if (androidClient.isConnected()) {
      try {
        androidClient.unsubscribe(topic);
      } catch (MqttException err) {
        Log.i(DEBUG_TAG, err.getLocalizedMessage());
      }
    } else {
      Log.i(DEBUG_TAG, "Could not unsubscribe. Client is not connected. Please connect first.");
    }
  }
Ejemplo n.º 3
0
  public void disconnect() {

    if (androidClient.isConnected()) {
      try {
        androidClient.disconnect();
        Log.i(DEBUG_TAG, "Client Disconnected");
      } catch (MqttException err) {
        Log.i(DEBUG_TAG, err.getLocalizedMessage());
      }
    } else {
      Log.i(DEBUG_TAG, "Client is already disconnected");
    }
  }
Ejemplo n.º 4
0
  public void publish(String topic, byte[] payload) {

    if (androidClient.isConnected()) {
      try {
        androidClient.publish(topic, payload, qualityOfService, false);
      } catch (MqttPersistenceException e) {
        Log.i(DEBUG_TAG, e.getLocalizedMessage());
      } catch (MqttException e) {
        Log.i(DEBUG_TAG, e.getLocalizedMessage());
      }
    } else {
      Log.i(DEBUG_TAG, "Could not publish. Client is not connected. Please connect first.");
    }
  }
Ejemplo n.º 5
0
  public boolean subscribe(final String topic) {

    if (androidClient.isConnected()) {
      try {
        androidClient.subscribe(topic, qualityOfService);
        isSubscribed = true;
        return true;
      } catch (MqttException err) {
        Log.i(DEBUG_TAG, err.getLocalizedMessage());
        return false;
      }
    } else {
      Log.i(DEBUG_TAG, "Could not subscribe. Client is not connected. Please connect first.");
      return false;
    }
  }