示例#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;
  }