我正在使用MQTT和Android,但似乎无法将客户端连接到代理。代码是这样的:
String clientId = MqttClient.generateClientId();
MqttAndroidClient client = new MqttAndroidClient(this, brokerIp, clientId);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
Log.i(TAG, "connectionLost");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.i(TAG, "messageArrived");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.i(TAG, "deliveryComplete");
}
});
client.connect().setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.i(TAG, "Connection success");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.i(TAG, "Connection failed: ", exception);
}
});
问题在于没有回调被触发。在详细模式下运行mosquitto时,此客户端没有任何连接进入,并检查日志我没有看到任何异常。
我在本地和云上尝试过,它没有连接到任何设备,而后端有一个客户端可以使用相同的主机和端口连接到这两个设备。 我想念什么?
答案 0 :(得分:0)
结果是,需要在清单中声明MqttService才能正确绑定到Paho Android服务:
<service android:name="org.eclipse.paho.android.service.MqttService"/>