Search code examples
androidpush-notificationmqmqtt

Java client can't receive message from Mosquitto on Android


  1. My Os is Windows 7,32bit.

  2. I install mosquitto-1.1.2-install-win32.exe.

  3. I don't change the mosquitto.conf file,so no topic prefix setting.

  4. Use Mosquitto to subscribe topic like(the subscription window):

    mosquitto_sub.exe -q 2 -t mytopic
    OR mosquitto_sub.exe -q 2 -t # 
    
  5. Use Mosquitto to publish a topic like(the publish window):

    mosquitto_pub.exe -q 2 -t mytopic -m “hello″
    
  6. Then in the subscription cmd window,I can receive "hello"

  7. I have been following Dale Lane's blog and "Android MQTT example project" to access Mosquitto.

    java client(MQTTDemo.java): 
    //i use mobile emulator
    editor.putString(“broker”, "10.0.2.2″); 
    editor.putString(“topic”, "mytopic”); 
    //or editor.putString(“topic”, “#”); '#' match any topic
    
  8. I test connectToBroker() in MQTTService.java, the connection is OK.

  9. I publish the topic "mytopic" again with Mosquitto.

  10. But,finally I cannot receive the message in the mobile emulator.

    Anybody know why or any other methods?

    Thanks a lot!


Solution

  • Depends at times on how the broker is configured. There are ways within the Mosquitto broker to specify a topic prefix on a per-channel basis. At other times, the subscription topic needs to be specified as generic. I'm not sure if either of these apply in your situation, but there are easy diagnostics and you do not describe using them so I'll offer them as a starting point.

    1. Easy way to tell is just to subscribe to # and then look at the topic strings that you receive.
    2. Always have a separate subscription monitoring the broker. For example, with WebSphere MQ I use the GUI to create a subscription to # before testing any publications. If I get the publication in the GUI but not the app it points to a different problem set than if I don't get anything at all.

    A better description of the problem will include some of this differential diagnostic. If you get the chance, please re-test and edit the question with the results.