MQTT paho java客户端不在qos 2上发布消息

时间:2016-04-18 07:19:58

标签: java mqtt paho

我正在使用MQTT paho java库尝试使用qos2将消息发布到主题。但这个话题并没有收到它。我已经遵循了所有要求,例如clean session = false,同样的客户端ID ......,但它似乎没有用。它仅适用于qos 0&谁能告诉我我的代码有什么问题?

     private int             brokerPortNumber     = 1883;
    private MqttPersistence usePersistence       = null;
        private boolean         cleanStart           = false;
       private int[]           qualitiesOfService   = { 2 } ;

      subscribeToTopic(uid);

      public void publishMessageToTopic(String message, String uid)
        {
            Boolean retained = false;
            if (isAlreadyConnected() == false) {
                Log.d(TAG, "mqtt, Unable to publish as we are not connected");
              Toast.makeText(this, "No internet connection", Toast.LENGTH_LONG).show();

               }
            else
            {
                try
                {

                    //  Log.d(TAG, "MQTT Publish Message Rcvd: " + message);


                    // String[] tps = { topicName };
                    // MqttPayload msg = new MqttPayload(message.getBytes());
                    byte[] msg = message.getBytes();




                 String value3=uid;



                    mqttClient.publish(uid, msg, 2, false);



                }
                catch (MqttNotConnectedException e)
                {
                    Log.e("mqtt", "subscribe failed - MQTT not connected", e);
                    Toast.makeText(this, "MQTT not connected", Toast.LENGTH_LONG).show();
                }
                catch (IllegalArgumentException e)
                {
                    Log.e("mqtt", "subscribe failed - illegal argument", e);
                }
                catch (MqttException e)
                {
                    Log.e("mqtt", "subscribe failed - MQTT exception", e);
                }
            }
        }

        /*
         * Send a request to the message broker to be sent messages published with
         *  the specified topic name. Wildcards are allowed.
         */
        private void subscribeToTopic(String topicName)
        {
            boolean subscribed = false;

            if (isAlreadyConnected() == false)
            {
                // quick sanity check - don't try and subscribe if we
                //  don't have a connection

                Log.e("mqtt", "Unable to subscribe as we are not connected");
            }
            else
            {
                try
                {
                    String[] topics = { topicName };
                    mqttClient.subscribe(topics, qualitiesOfService);

                    subscribed = true;
                }
                catch (MqttNotConnectedException e)
                {
                    Log.e("mqtt", "subscribe failed - MQTT not connected", e);
                }
                catch (IllegalArgumentException e)
                {
                    Log.e("mqtt", "subscribe failed - illegal argument", e);
                }
                catch (MqttException e)
                {
                    Log.e("mqtt", "subscribe failed - MQTT exception", e);
                }
            }

            if (subscribed == false)
            {

                broadcastServiceStatus("Unable to subscribe");


              //notifyUser("Unable to subscribe", "MQTT", "Unable to subscribe");
            }
        }


      public void publishArrived(String topic, byte[] payloadbytes, int qos, boolean retained)
        {

            // we protect against the phone switching off while we're doing this
            //  by requesting a wake lock - we request the minimum possible wake
            //  lock - just enough to keep the CPU running until we've finished
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MQTT");
            wl.acquire();

            //
            //  I'm assuming that all messages I receive are being sent as strings
            //   this is not an MQTT thing - just me making as assumption about what
            //   data I will be receiving - your app doesn't have to send/receive
            //   strings - anything that can be sent as bytes is valid

            String messageBody = new String(payloadbytes);


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            //
            // inform the user (for times when the Activity UI isn't running)
            //   that there is new data available
            // notifyUser("New message received", messageBody);



            // receiving this message will have kept the connection alive for us, so
            //  we take advantage of this to postpone the next scheduled ping
            scheduleNextPing();

            // we're finished - if the phone is switched off, it's okay for the CPU
            //  to sleep now
            wl.release();
        }

//Connection to broker
 private int             brokerPortNumber     = 1883;
private MqttPersistence usePersistence       = null;
    private boolean         cleanStart           = false;
   private int[]           qualitiesOfService   = { 2 } ;

  subscribeToTopic(uid);

  public void publishMessageToTopic(String message, String uid)
    {
        Boolean retained = false;
        if (isAlreadyConnected() == false) {
            Log.d(TAG, "mqtt, Unable to publish as we are not connected");
          Toast.makeText(this, "No internet connection", Toast.LENGTH_LONG).show();

           }
        else
        {
            try
            {

                //  Log.d(TAG, "MQTT Publish Message Rcvd: " + message);


                // String[] tps = { topicName };
                // MqttPayload msg = new MqttPayload(message.getBytes());
                byte[] msg = message.getBytes();




             String value3=uid;



                mqttClient.publish(uid, msg, 2, false);



            }
            catch (MqttNotConnectedException e)
            {
                Log.e("mqtt", "subscribe failed - MQTT not connected", e);
                Toast.makeText(this, "MQTT not connected", Toast.LENGTH_LONG).show();
            }
            catch (IllegalArgumentException e)
            {
                Log.e("mqtt", "subscribe failed - illegal argument", e);
            }
            catch (MqttException e)
            {
                Log.e("mqtt", "subscribe failed - MQTT exception", e);
            }
        }
    }

    /*
     * Send a request to the message broker to be sent messages published with
     *  the specified topic name. Wildcards are allowed.
     */
    private void subscribeToTopic(String topicName)
    {
        boolean subscribed = false;

        if (isAlreadyConnected() == false)
        {
            // quick sanity check - don't try and subscribe if we
            //  don't have a connection

            Log.e("mqtt", "Unable to subscribe as we are not connected");
        }
        else
        {
            try
            {
                String[] topics = { topicName };
                mqttClient.subscribe(topics, qualitiesOfService);

                subscribed = true;
            }
            catch (MqttNotConnectedException e)
            {
                Log.e("mqtt", "subscribe failed - MQTT not connected", e);
            }
            catch (IllegalArgumentException e)
            {
                Log.e("mqtt", "subscribe failed - illegal argument", e);
            }
            catch (MqttException e)
            {
                Log.e("mqtt", "subscribe failed - MQTT exception", e);
            }
        }

        if (subscribed == false)
        {

            broadcastServiceStatus("Unable to subscribe");


          //notifyUser("Unable to subscribe", "MQTT", "Unable to subscribe");
        }
    }


  public void publishArrived(String topic, byte[] payloadbytes, int qos, boolean retained)
    {

        // we protect against the phone switching off while we're doing this
        //  by requesting a wake lock - we request the minimum possible wake
        //  lock - just enough to keep the CPU running until we've finished
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MQTT");
        wl.acquire();

        //
        //  I'm assuming that all messages I receive are being sent as strings
        //   this is not an MQTT thing - just me making as assumption about what
        //   data I will be receiving - your app doesn't have to send/receive
        //   strings - anything that can be sent as bytes is valid

        String messageBody = new String(payloadbytes);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        //
        // inform the user (for times when the Activity UI isn't running)
        //   that there is new data available
        // notifyUser("New message received", messageBody);



        // receiving this message will have kept the connection alive for us, so
        //  we take advantage of this to postpone the next scheduled ping
        scheduleNextPing();

        // we're finished - if the phone is switched off, it's okay for the CPU
        //  to sleep now
        wl.release();
    }

与经纪人的联系

private void defineConnectionToBroker(String brokerHostName)
        {
            String mqttConnSpec = "tcp://" + brokerHostName + "@" + brokerPortNumber;

            try
            {
                // define the connection to the broker
                mqttClient = MqttClient.createMqttClient(mqttConnSpec, usePersistence);

                // register this client app has being able to receive messages
                mqttClient.registerSimpleHandler(this);
            }
            catch (MqttException e)
            {
                // something went wrong!
                mqttClient = null;
                connectionStatus = MQTTConnectionStatus.NOTCONNECTED_UNKNOWNREASON;

                //
                // inform the app that we failed to connect so that it can update
                //  the UI accordingly
                broadcastServiceStatus("Invalid connection parameters");

                //
                // inform the user (for times when the Activity UI isn't running)
                //   that we failed to connect
                // notifyUser("Unable to connect", "MQTT", "Unable to connect");
            }
        }

0 个答案:

没有答案