IMqttActionListener回调多次

时间:2018-10-08 14:23:39

标签: java android mqtt paho

我想创建一个在后台运行的服务。
看起来不错,但是当再次调用RunMqttProcess的onSuccess方法时。
在第一个->发布一条消息中
第二个->发布两个消息
...
第四->发布4条消息。

locationScenes 只有一个元素。

public class MqttService {
   static IMqttActionListener actionListener = null;
   public static boolean RunMqttProcess(final Context context, final Config config, final Location location, final ArrayList<LocationScene> locationScenes) {
           String clientId = "ThoMi_" + getMacAddress();
           HashMap mqttConfig = config.getMqttConfig();
           Log.d(TAG, "clientId: " + clientId);
           final MqttAndroidClient mqttAndroidClient = new MqttAndroidClient(context, mqttConfig.get("url").toString(), clientId);
           MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
           mqttConnectOptions.setUserName(mqttConfig.get("userName").toString());
           mqttConnectOptions.setPassword(mqttConfig.get("password").toString().toCharArray());
           mqttConnectOptions.setCleanSession(true);
           for (final LocationScene locationScene : locationScenes) {final Double meter = Convert.measure(location.getLatitude(), location.getLongitude(), locationScene.getLatitude(), locationScene.getLongitude());
               Boolean conditionTime = checkConditions();
               //......
               if (conditionTime && meter < locationScene.getRadius() && 0 == locationScene.getControlled() && !locationScene.getDisabled()) {
                   locationScene.setControlled(System.currentTimeMillis());
                   LocationSceneDAO dao = DAOFactory.createLocationSceneDAO(context);
                   dao.editLocationScene(locationScene);
                   actionListener = null;
                   actionListener = new IMqttActionListener() {
                       @Override
                       public void onSuccess(IMqttToken asyncActionToken) {
                           DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
                           disconnectedBufferOptions.setBufferEnabled(true);
                           disconnectedBufferOptions.setBufferSize(100);
                           disconnectedBufferOptions.setPersistBuffer(false);
                           disconnectedBufferOptions.setDeleteOldestMessages(false);
                           mqttAndroidClient.setBufferOpts(disconnectedBufferOptions);
                           try {
                               for (HashMap<String, String> device : locationScene.getDevices()) {
                                   MqttMessage message = new MqttMessage();
                                   message.setRetained(false);
                                   message.setPayload(device.get("message").getBytes());
                                   mqttAndroidClient.publish(device.get("topic"), message);
                                   if (!mqttAndroidClient.isConnected()) {
                                       Log.d(TAG, mqttAndroidClient.getBufferedMessageCount() + " messages in buffer.");
                                   }
                               }
                           } catch (MqttException e) {
                               Log.d(TAG, "[postMQTT]:  Error Publishing: " + e.getMessage());
                               e.printStackTrace();
                           }
                       }
                       @Override
                       public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                           Log.d(TAG, "[postMQTT]: Failed to connect to " + config.getMqttConfig().get("url"));
                       }
                   };
                   mqttAndroidClient.connect(mqttConnectOptions, null, actionListener);

               }
           }
           if (mqttAndroidClient.isConnected()) {
               mqttAndroidClient.disconnect();
           }
       } catch (MqttException e) {
           Log.d(TAG, e.getCause().getMessage());
       }
       return true;
   }
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

可能已经晚了,但是请确保您要断开MQTT客户端的连接。我的问题是,每次我启动应用程序时,都会再次建立连接,因此,多个客户端在同一clientID上进行连接。