使用android时,RSMB MQTT代理上的客户端标识符出现套接字错误

时间:2013-04-17 15:58:44

标签: android mqtt

我正在为android构建一个简单的MQTT客户端,我在RMBS控制台上收到“客户端标识符的套接字错误”错误。这只发生在客户端的android实现中(我还创建了一个Java桌面客户端并且运行没有问题)。对于Android客户端,我正在起诉Paho Java客户端库。这是我的代码:

这是Android客户端:

package com.example.mqttdroid;

import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MQTTClient extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mqttclient);
//  new BackTask().execute(); not used because (seems to be the problem)
  MqttConnectOptions conOpts = new MqttConnectOptions();
            conOpts.setKeepAliveInterval(30);
            conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
            client.connect(conOpts);

            client.subscribe("/House/Kitchen/Bulb");
            client.setCallback( new MqttCallback() {



                @Override
                public void connectionLost(Throwable arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void deliveryComplete(IMqttDeliveryToken arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void messageArrived(String arg0, MqttMessage arg1)
                        throws Exception {
                    // TODO Auto-generated method stub
                    Toast.makeText(Main.this, arg0.toString(), Toast.LENGTH_SHORT).show();
                }
            });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.mqttclient, menu);
    return true;
}

/*public class BackTask extends AsyncTask<Void, Void, Void>{

    private MqttClient client;
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            client = new MqttClient("tcp://"Ip of machine running RSMB":1883", "ANDROID1", new MemoryPersistence());

            client.connect();
            client.subscribe("House/Kitchen/Bulb");


        } catch (MqttException e) {
            // TODO Auto-generated catch block
            Log.e("ERROR", "NOT CONNECTED");
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        try {
            client.setCallback( new MqttCallback() {

                @Override
                public void messageArrived(MqttTopic arg0, MqttMessage arg1)
                        throws Exception {
                    // TODO Auto-generated method stub
                    Toast.makeText(MQTTClient.this, arg0.toString(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void deliveryComplete(MqttDeliveryToken arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void connectionLost(Throwable arg0) {
                    // TODO Auto-generated method stub

                }
            });
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 }*/

这是桌面Java客户端:

 import org.eclipse.paho.client.mqttv3.MqttCallback;
 import org.eclipse.paho.client.mqttv3.MqttClient;
 import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
 import org.eclipse.paho.client.mqttv3.MqttException;
 import org.eclipse.paho.client.mqttv3.MqttMessage;
 import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
 import org.eclipse.paho.client.mqttv3.MqttTopic;
 import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;



 public class MQTTBaseClass {

/**
 * @param args
 */

public static void main(String[] args) {
    // TODO Auto-generated method stub
    MqttClientPersistence persistence;
    try {

        MqttClient client = new MqttClient("tcp://localhost:1883", "PC",new      MemoryPersistence());
        MqttConnectOptions conOpts = new MqttConnectOptions();
        conOpts.setKeepAliveInterval(30);
        conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
        client.connect(conOpts);
        MqttMessage msg = new MqttMessage("hello".getBytes());
        msg.setQos(0);
        msg.setRetained(true);
        MqttTopic topic = client.getTopic("House/Kitchen/Bulb");
        client.subscribe("House/Kitchen/Bulb");

        try {
            client.setCallback( new MqttCallback() {

                @Override
                public void messageArrived(MqttTopic arg0, MqttMessage arg1)
                        throws Exception {
                    // TODO Auto-generated method stub
                    System.out.println(arg1.toString());
                }

                @Override
                public void deliveryComplete(MqttDeliveryToken arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void connectionLost(Throwable arg0) {
                    // TODO Auto-generated method stub

                }
            });
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        topic.publish(msg);


    } catch (MqttPersistenceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MqttException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}






 }

只是几点说明:

我通过WiFi在我的Android设备上连接,当我运行Java Dekstop客户端时,我的桌面也是如此。

Java Destop Client与RSMB在同一台计算机上运行

Java Desktop Client创建并显示主题“House / Kitchen / Bulb”并发送带有字符串“Hello”的消息

Android客户端还订阅“House / Kitchen / Bulb”并尝试使用收到的消息显示Toast。

我已经在android清单

上添加了Internet权限

Android设备似乎很好地连接到代理,但是一旦我初始化Java桌面服务客户端(或Eclipse中的Paho客户端插件并发布消息),我就会收到提到的错误。

我在RSMB运行的同一台机器上使用模拟器运行应用程序,我也遇到了同样的错误。

可能是什么问题?

更新:

最初,我有一个“主线程上的网络”异常,所以我将连接操作移动到AsyncTask。现在看来,当我用Java客户端发布消息时,Android客户端仍然连接(Asynctask可能一直在创建问题),但似乎没有调用MqttCallback()的messageArrived()。

更新2:

我设法让它发挥作用。这是我现在使用的代码:

package com.example.mqttphone;

*import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;*

public class Main extends Activity {
protected static String msg;
public MqttClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {

        client = new MqttClient("tcp://10.1.201.27:1883", "ANDROID1", new MemoryPersistence());

        MqttConnectOptions conOpts = new MqttConnectOptions();
        conOpts.setKeepAliveInterval(30);
        conOpts.setWill(client.getTopic("Error"), "something bad happened".getBytes(), 1, true);
        client.setCallback( new MqttCallback() {



            @Override
            public void connectionLost(Throwable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void messageArrived(String arg0, MqttMessage arg1)
                    throws Exception {
                // TODO Auto-generated method stub
            Main.msg = arg1.toString();
            Main.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Toast.makeText(Main.this, msg, Toast.LENGTH_LONG).show();
                }
            });
                Log.e("MESSAGE RECEIVED", arg1.toString());

            }
        });
        client.connect(conOpts);
        //MqttMessage msg = new MqttMessage("ANDROID MESSAGE".getBytes());
        //client.getTopic("world").publish(msg);

        if(client.isConnected()){

        client.subscribe("/House/Kitchen/Bulb");


            Toast.makeText(this, "CONNECTED", Toast.LENGTH_SHORT).show();

        }



    } catch (MqttException e) {
        // TODO Auto-generated catch block
        Log.e("ERROR", "NOT CONNECTED");
        e.printStackTrace();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

2 个答案:

答案 0 :(得分:3)

您好我已经使用了您的代码,但是我无法将我的paho客户端连接到我的apollo服务器,我收到了以下错误。

 I/global(677): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
 E/ERROR(677): NOT CONNECTED
 W/System.err(677):  (32109) - java.io.EOFException
 W/System.err(677):     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:127)
 W/System.err(677):     at java.lang.Thread.run(Thread.java:1096)
 W/System.err(677): Caused by: java.io.EOFException
 W/System.err(677):     at java.io.DataInputStream.readFully(DataInputStream.java:266)
 W/System.err(677):     at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:63)
 W/System.err(677):     at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:94)
 W/System.err(677):     ... 1 more

感谢
吉里什

答案 1 :(得分:1)

我得到了它的工作。这是github存储库的link,每个人都可以找到实现此代码的项目。它允许用户通过语音识别使用他们的Android设备远程打开LED灯。