0

当我使用 wifi 模块在计算机和 android 设备之间发送/接收数据时遇到问题。我的电脑可以发送/接收来自 android 的数据,但 android 设备无法从电脑接收数据,它只能发送什么问题?我将计算机设置为 TCP Server,而 android 是 TCP Client。我认为它在 DataInputStrem 中出错,但我不知道如何解决。

public class TCPClient {

    private String serverMessage;
    public static final String SERVERIP = "192.168.0.1"; //your computer IP address
    public static final int SERVERPORT = 6000;
    private OnMessageReceived mMessageListener = null;
    private boolean mRun = false;

    DataOutputStream out;
    DataInputStream in;

    /**
     *  Constructor of the class. OnMessagedReceived listens for the messages received from server
     */
    public TCPClient(OnMessageReceived listener) {
        mMessageListener = listener;
    }

    /**
     * Sends the message entered by client to the server
     * @param message text entered by client
     */
    public void sendMessage(String message){
        if (out != null) {
            try {
                out.writeUTF(message);
                out.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public void stopClient(){
        mRun = false;
    }

    public void run() {

        mRun = true;

        try {
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);

            Log.e("TCP Client", "C: Connecting...");

            //create a socket to make the connection with the server
            Socket socket = new Socket(serverAddr, SERVERPORT);

            try {
                //send the message to the server
                out = new DataOutputStream(socket.getOutputStream());
                Log.e("TCP Client", "C: Sent.");
                Log.e("TCP Client", "C: Done.");

                //receive the message which the server sends back
                in = new DataInputStream(socket.getInputStream());
                Log.e("TCP Client x", in.readUTF());
                //in this while the client listens for the messages sent by the server
                while (mRun) {
                    serverMessage = in.readUTF();
                    if (serverMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                        mMessageListener.messageReceived(serverMessage);
                    }
                    serverMessage = null;
                }
                Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

            } catch (Exception e) {

                Log.e("TCP", "S: Error", e);

            } finally {
                //the socket must be closed. It is not possible to reconnect to this socket
                // after it is closed, which means a new socket instance has to be created.
                socket.close();
            }

        } catch (Exception e) {

            Log.e("TCP", "C: Error", e);

        }

    }
    //Declare the interface.
    //class at on asynckTask doInBackground
    public interface OnMessageReceived {
        public void messageReceived(String message);
    }
}
4

1 回答 1

0

谢谢大家的回答,很抱歉这个问题不清楚。我的项目是通过我的电子板连接Android设备和电子板,包括wifi模块“HLKM03”。我通过以下方式测试连接

1 我看到使用 TCPClient 应用程序在客户端(Android)中发送/接收数据。和
2 我在服务器端(电子板 + Wifi 接入点)看到数据发送/接收,所以我通过这个解决方案“wifi + 电子板 --> usb2RS232 --> 计算机(docklight 程序)”检查数据传输。来自所有。现在我可以通过更改此代码来解决我所有的问题

 try {
            //send the message to the server
            out = new DataOutputStream(socket.getOutputStream());
            Log.e("TCP Client", "C: Sent.");
            Log.e("TCP Client", "C: Done.");

            //receive the message which the server sends back
            in = new DataInputStream(socket.getInputStream());
            //Log.e("TCP Client x", in.readUTF());
            //in this while the client listens for the messages sent by the server
            while (mRun) {
                //serverMessage = in.readUTF();
                //TCPClient localClientActivity = TCPClient.this;
                int j = in.read(buffer);
                serverMessage = bytesToHexString(buffer, j);

                if (serverMessage != null && mMessageListener != null) {
                    //call the method messageReceived from MyActivity class
                    mMessageListener.messageReceived(serverMessage);
                }
                serverMessage = null;
            }
            Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

        } catch (Exception e) {

            Log.e("TCP", "S: Error", e);

        } finally {
            //the socket must be closed. It is not possible to reconnect to this socket
            // after it is closed, which means a new socket instance has to be created.
            socket.close();
        }

并添加函数 chage bytetoHexString 用于转换数据从服务器接收数据。它已经可以帮助我的 android 从我的电子板上接收数据。再次感谢你。

于 2013-09-24T08:46:53.953 回答