我想编写一个连接到 BLE 设备并通过 MQTT 将消息转发到 MQTT 代理的小型 IoT 代理。似乎唯一存在的 Android MQTT 服务是 Eclipse Paho。我正在尝试这个,但没有收到任何消息。而是抛出一个异常......
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();
}
错误是这样的:
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)
任何想法?