简体   繁体   中英

MQTT Implementation Issue (Dale Lane Example)

I am running into an issue with the implementation of the Dale Lane MQTT solution.

I cannot seem to figure out how to retrieve the published message from the MQTT Client I am using.

I am not sure if I am utilizing the the onReceive() method incorrectly, but for now all I would like to do is log the broadcasted messages.

http://dalelane.co.uk/blog/?p=1599

The service I have implemented is exactly as listed here, I have no errors.

    public class MQTTNotifier extends Activity implements OnClickListener {

  String preferenceBrokerHost, preferenceBrokerTopic;

  private StatusUpdateReceiver statusUpdateIntentReceiver;
  private MQTTMessageReceiver messageIntentReceiver;
  private EditText etBroker, etTopic;

  Button btnSubscribe, btnStopService;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mqttnotifier);
    initValues();
    // startService();
  }

  private void initValues() {

    btnSubscribe = (Button) findViewById(R.id.btnSubscribe);
    btnSubscribe.setOnClickListener(this);

    btnStopService = (Button) findViewById(R.id.btnStopService);
    btnStopService.setOnClickListener(this);



    // if statement to see if sharedpreferences exist, if so reopen recievers
    /*
     * statusUpdateIntentReceiver = new StatusUpdateReceiver(); IntentFilter intentSFilter = new
     * IntentFilter(MQTTService.MQTT_STATUS_INTENT); registerReceiver(statusUpdateIntentReceiver,
     * intentSFilter);
     * 
     * messageIntentReceiver = new MQTTMessageReceiver(); IntentFilter intentCFilter = new
     * IntentFilter(MQTTService.MQTT_MSG_RECEIVED_INTENT); registerReceiver(messageIntentReceiver,
     * intentCFilter);
     */


  }

  public class StatusUpdateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle notificationData = intent.getExtras();
      String newStatus = notificationData.getString(MQTTService.MQTT_STATUS_MSG);


    }
  }
  public class MQTTMessageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle notificationData = intent.getExtras();
      String newTopic = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_TOPIC);
      String newData = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_MSG);
      Log.e("NEW TOPIC", newTopic);
      Log.e("NEW DATA", newData);


    }
  }

  @Override
  protected void onDestroy() {
    unregisterReceiver(statusUpdateIntentReceiver);
    unregisterReceiver(messageIntentReceiver);

  }

  private void startService() {
    Intent svc = new Intent(this, MQTTService.class);
    startService(svc);
  }

  private void stopService() {
    Intent svc = new Intent(this, MQTTService.class);
    stopService(svc);
  }

  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
      NotificationManager mNotificationManager =
          (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      mNotificationManager.cancel(MQTTService.MQTT_NOTIFICATION_UPDATE);
    }
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
      case R.id.btnSubscribe:

        etBroker = (EditText) findViewById(R.id.etBroker);
        etTopic = (EditText) findViewById(R.id.etTopic);
        preferenceBrokerHost = etBroker.getText().toString().trim();
        preferenceBrokerTopic = etBroker.getText().toString().trim();

        createSharedPreferences(preferenceBrokerHost, preferenceBrokerTopic);
        establishRecievers();
        startService();
        break;

      case R.id.btnStopService:
        stopService();
    }

  }

  private void createSharedPreferences(String broker, String topic) {
    SharedPreferences settings = getSharedPreferences(MQTTService.APP_ID, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("broker", broker);
    editor.putString("topic", topic);
    editor.commit();

  }

  private void establishRecievers() {
    statusUpdateIntentReceiver = new StatusUpdateReceiver();
    IntentFilter intentSFilter = new IntentFilter(MQTTService.MQTT_STATUS_INTENT);
    registerReceiver(statusUpdateIntentReceiver, intentSFilter);


    messageIntentReceiver = new MQTTMessageReceiver();
    IntentFilter intentCFilter = new IntentFilter(MQTTService.MQTT_MSG_RECEIVED_INTENT);
    registerReceiver(messageIntentReceiver, intentCFilter);
  }
}

It turns out that the issue was in regards to a simple mistake that was in regards to this line.

    preferenceBrokerTopic = etBroker.getText().toString().trim();

I corrected the issue and now it corrects properly because it was not subscribing to the correct topic.

preferenceBrokerTopic = etTopic.getText().toString().trim();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM