MQTT实现问题(Dale Lane示例)

时间:2015-02-09 20:10:25

标签: java android mqtt

我遇到了Dale Lane MQTT解决方案实施的问题。

我似乎无法弄清楚如何从我正在使用的MQTT客户端检索已发布的消息。

我不确定我是否错误地使用了onReceive()方法,但是现在我要做的就是记录广播的消息。

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

我实施的服务与此处列出的完全相同,我没有错误。

    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);
  }
}

1 个答案:

答案 0 :(得分:0)

事实证明,问题是关于这条线的一个简单错误。

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

我更正了问题,现在它已正确纠正,因为它没有订阅正确的主题。

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