MQTT 实施问题(Dale Lane 示例)

MQTT Implementation Issue (Dale Lane Example)

我 运行 遇到 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);
  }
}

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

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

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

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