Far*_*d T 9 service android publish mqtt paho
我想将消息从android发布service到本地服务器.以下是基于此处片段的最简单形式的代码部分.
MemoryPersistence memPer;
MqttAndroidClient client;
@Override
public IBinder onBind(Intent intent) {
memPer = new MemoryPersistence();
client = new MqttAndroidClient(this, "tcp://192.168.1.42:1883", "clientid", memPer);
try {
client.connect(null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken mqttToken) {
Log.i("MQTT", "Client connected");
Log.i("MQTT", "Topics=" + mqttToken.getTopics());
MqttMessage message = new MqttMessage("Hello, I am Android Mqtt Client.".getBytes());
message.setQos(2);
message.setRetained(false);
try {
client.publish("messages", message);
Log.i("MQTT", "Message published");
client.disconnect();
Log.i("MQTT", "client disconnected");
} catch (MqttPersistenceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
Log.i("MQTT", "Client connection failed: " + arg1.getMessage());
}
});
} catch (MqttException e) {
e.printStackTrace();
}
return mBinder;
}
Run Code Online (Sandbox Code Playgroud)
但总是调用onFailure函数,我得到错误:
I/MQTT? Client connection failed: cannot start service org.eclipse.paho.android.service.MqttService
Run Code Online (Sandbox Code Playgroud)
显然由图书馆返回,因为'listener!= null',410行.使用调试器,它显示'listener = SensorLoggerService $ 1 @ 3634'.SensorLoggerService是我的服务.
什么可能出错?非常感谢.
Dar*_*llo 17
对我来说同样的问题,在我的情况下问题是<service>标签在标签之外<application>.
一开始我有这个:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<service android:name="org.eclipse.paho.android.service.MqttService">
</service>
...
<application
android:name="com.mycompany.myapp" ... >
...
</application>
Run Code Online (Sandbox Code Playgroud)
然后我改为:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<application
android:name="com.mycompany.myapp" ... >
...
<service android:name="org.eclipse.paho.android.service.MqttService">
</service>
</application>
Run Code Online (Sandbox Code Playgroud)
一切正常!
您还需要添加INTERNET和ACCESS_NETWORK_STATE权限,否则您将无法连接.
HTH
经过几个小时的尝试,我成功地连接到了MqttClient而不是MqttAndroidClient。我仍然不知道为什么MqttAndroidClient失败。
以下是一些提示:
服务类应该实现MqttCallback
清单(“AndroidManifest.xml”)必须至少包含以下内容:
<!-- Internet permission -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- MqttService -->
<service android:name="org.eclipse.paho.android.service.MqttService" />
Run Code Online (Sandbox Code Playgroud)
修改后的代码:
MemoryPersistence memPer;
MqttClient client;
@Override
public IBinder onBind(Intent intent) {
memPer = new MemoryPersistence();
try
{
client = new MqttClient("tcp://192.168.1.42:1883", MqttClient.generateClientId(), null);
client.setCallback(this);
}
catch (MqttException e1)
{
e1.printStackTrace();
}
MqttConnectOptions options = new MqttConnectOptions();
try
{
client.connect(options);
}
catch (MqttException e)
{
Log.d(getClass().getCanonicalName(), "Connection attempt failed with reason code = " + e.getReasonCode() + ":" + e.getCause());
}
// Now, try to publish a message
String msg = "Hello, I am Android Mqtt Client.";
try
{
MqttMessage message = new MqttMessage();
message.setQos(1);
message.setPayload(msg.getBytes());
client.publish("sensors/test", message);
}
catch (MqttException e)
{
Log.d(getClass().getCanonicalName(), "Publish failed with reason code = " + e.getReasonCode());
}
return mBinder;
}
@Override
public void connectionLost(Throwable cause)
{
Log.d("MQTT", "MQTT Server connection lost" + cause.getMessage());
}
@Override
public void messageArrived(String topic, MqttMessage message)
{
Log.d("MQTT", "Message arrived:" + topic + ":" + message.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken token)
{
Log.d("MQTT", "Delivery complete");
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅这个不错的指南:https://developer.motorolasolutions.com/docs/DOC-2315
| 归档时间: |
|
| 查看次数: |
9766 次 |
| 最近记录: |