Ejemplo n.º 1
0
  private boolean sendInfectionMessage(boolean entering) {
    if (mqttClient != null && mqttClient.isConnected()) {
      try {
        JSONArray a = gameDatabase.findAllVirusDataAsJson();
        JSONObject o = new JSONObject();
        if (entering) o.put("entering", currentTopic);
        o.put("sender", clientId);
        o.put("viruses", a);
        String payload = o.toString();
        Log.d(LOG_TAG, "publishing to " + currentTopic + ": " + payload);
        IMqttDeliveryToken token =
            mqttClient.publish(currentTopic, new MqttMessage(payload.getBytes()));
        token.setActionCallback(
            new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken asyncActionToken) {
                sendEnterMessage = false;
              }

              @Override
              public void onFailure(IMqttToken asyncActionToken, Throwable exception) {}
            });
        return true;
      } catch (MqttException e) {
        //
      } catch (JSONException e) {
        Log.e(LOG_TAG, "JSONException", e);
      }
    }
    return false;
  }
 // region LocationListener
 @Override
 public void onLocationChanged(Location location) {
   String msg =
       String.format(
           "{\"id\":\"%s\",\"x\":%.9f,\"y\":%.9f}",
           id, location.getLongitude(), location.getLatitude());
   try {
     client.publish("Likaci/MqttMap", msg.getBytes(), 0, false);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 3
0
 @Override
 public void onSuccess(IMqttToken asyncActionToken) {
   Log.d(TAG, "onSuccess");
   try {
     MqttMessage message = new MqttMessage(new String(client.getClientId()).getBytes());
     message.setQos(qos);
     message.setRetained(true);
     client.publish("/users", message);
     client.subscribe("/users", qos);
   } catch (MqttException e) {
     e.printStackTrace();
   }
 }
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.");
    }
  }