3

I would like to write a small IoT Proxy that connects to BLE devices and forwards messages via MQTT to a MQTT broker. It seems the only Android MQTT Service that exists is Eclipse Paho. I am trying this, but no message is received. Instead an Exception is thrown....

MqttAndroidClient client = new MqttAndroidClient(getApplicationContext(),"tcp://xx.xx.xx.xx:1883", "androidClient");
    try {
        client.connect();
        client.publish("box/1", new MqttMessage("blubb".getBytes()));
    } catch (MqttException e) {
        e.printStackTrace();
    }

The error is this:

03-18 16:35:03.135  28618-28618/com.hybris.labs.moto E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.hybris.labs.moto, PID: 28618
java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttDeliveryToken org.eclipse.paho.android.service.MqttService.publish(java.lang.String, java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage, java.lang.String, java.lang.String)' on a null object reference
        at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:812)
        at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:668)
        at org.eclipse.paho.android.service.MainActivity$1.onServiceConnected(MainActivity.java:169)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1203)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1220)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)

Any idea?

ralight
  • 11,033
  • 3
  • 49
  • 59
Sven Haiges
  • 2,636
  • 5
  • 42
  • 54
  • 2
    Ok, I was totally overengineering. I was binding to the service etc.but it seems so easy: try { MqttClient client = new MqttClient("tcp://.xx.xx.xx.xx:1883", "androidClient", new MemoryPersistence()); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(true); client.connect(options); client.getTopic("msg/1").publish("BLUBB".getBytes(), 0, false); } catch (MqttException e) { e.printStackTrace(); } – Sven Haiges Mar 18 '15 at 16:00
  • The only problem is subscription wiht the Android client. I try to set a callback and then call client.subscribe() - any code someone can show? – Sven Haiges Mar 18 '15 at 16:09
  • 1
    This looks like it could solve your problem: http://stackoverflow.com/a/26377973/383761 – Hugh Jeffner Apr 10 '15 at 14:56

0 Answers0