2015-09-05 236 views
3

我想從android service發佈消息到本地服務器。這裏是我的代碼的一部分,最簡單的形式是基於here的片段。Paho MqttAndroidClient.connect總是失敗

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; 
} 

但onFailure處函數總是叫我得到的錯誤:

I/MQTT﹕ Client connection failed: cannot start service org.eclipse.paho.android.service.MqttService 

顯然是由庫,因爲 '!聽者= NULL',Line 410返回。使用調試器,它顯示'listener = SensorLoggerService $ 1 @ 3634'。 SensorLoggerService是我的服務。

任何想法可能會出錯?非常感謝。

回答

0

經過幾個小時的嘗試,我設法連接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" /> 

修改後的代碼:

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"); 
} 

欲瞭解更多詳情,請參閱本好指南:https://developer.motorolasolutions.com/docs/DOC-2315

+0

你並不需要添加<服務機器人:名字=「org.eclipse.paho.android.service.MqttService」 />在AndroidManifest.xml這個解決方案,因爲你用泛美衛生組織庫同步任務。要使用android服務,您應該使用MqttAndroidClient而不是MqttClient。 –

9

同樣的問題對我來說,在我的情況下,問題是,<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> 

然後,我改變這樣的:

<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> 

和一切工作!

您還需要添加INTERNETACCESS_NETWORK_STATE權限,否則您將無法連接。

HTH

+0

我有同樣的問題,這解決了它! – Brett