0

我和我的团队正在尝试将 Android 手机连接到WiFly板。我们无法连接它。是否某些手机不支持此功能或支持此功能,我们必须做什么才能执行此连接?

我们已经尝试了一些事情,但仍然无法正常工作。如果您需要更多信息,请告诉我。

4

4 回答 4

1

我有一个基于 WiFly 模块的项目,并成功开发了一个应用程序,该应用程序通过 wifi 网络与模块建立 tcp 和 udp 连接。不,我不需要将手机创建为接入点。我可以使用无线路由器作为接入点从手机与 WiFly 通信。我也可以让它进入命令模式,这样我就可以编辑它的配置。如果您仍在寻找可行的解决方案,我很乐意为您提供我的代码。

编辑:下面是我制作的应用程序的完整代码。它已被用于在家庭自动化项目中连接到 WiFly。

主要活动

public class MainActivity extends Activity {

    private ToggleButton toggleButton1;
    private TcpClient mTcpClient;
    private Spinner spinner1, spinner2,spinner3,spinner4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        addItemsOnSpinner2();
        addItemsOnSpinner3();
        addItemsOnSpinner4();

        new connectTask().execute("");
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        Button button_send = (Button) findViewById(R.id.button_send);
        button_send.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view) {

                String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + "ON";

                //sends the message to the server
                if (mTcpClient != null) {
                    mTcpClient.sendMessage(message);
                }
            }
            });
            Button button_send1 = (Button) findViewById(R.id.button_send1);
            button_send1.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + "OFF";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_intensity = (Button) findViewById(R.id.button_intensity);
            button_intensity.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = String.valueOf(spinner1.getSelectedItem()) + " " + String.valueOf(spinner2.getSelectedItem()) + " " + String.valueOf(spinner3.getSelectedItem());

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_turnon = (Button) findViewById(R.id.button_turnon);
            button_turnon.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = String.valueOf(spinner4.getSelectedItem()) + " ON";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_turnoff = (Button) findViewById(R.id.button_turnoff);
            button_turnoff.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = String.valueOf(spinner4.getSelectedItem()) + " OFF";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
            toggleButton1.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {
                    // Is the toggle on?
                    boolean on = ((ToggleButton) view).isChecked();

                    if (on) {
                        String message = "$$$";

                        //sends the message to the server
                        if (mTcpClient != null) {
                            mTcpClient.sendMessage(message);
                        }
                    } else {
                        String message = "exit\r";

                        //sends the message to the server
                        if (mTcpClient != null) {
                            mTcpClient.sendMessage(message);
                        }
                    }
                }
                });
            Button button_led1 = (Button) findViewById(R.id.button_led1);
            button_led1.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "set sys iofunc 0x1\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_led2 = (Button) findViewById(R.id.button_led2);
            button_led2.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "set sys iofunc 0x2\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_led3 = (Button) findViewById(R.id.button_led3);
            button_led3.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "set sys iofunc 0x4\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_reset = (Button) findViewById(R.id.button_reset);
            button_reset.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "set sys iofunc 0x0\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_save = (Button) findViewById(R.id.button_save);
            button_save.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "save\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
            Button button_reboot = (Button) findViewById(R.id.button_reboot);
            button_reboot.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View view) {

                    String message = "reboot\r";

                    //sends the message to the server
                    if (mTcpClient != null) {
                        mTcpClient.sendMessage(message);
                    }
                }
            });
    }


         // add items into spinner dynamically
            public void addItemsOnSpinner2() {

            spinner2 = (Spinner) findViewById(R.id.spinner2);
            List<String> list = new ArrayList<String>();
            list.add("load1");
            list.add("load2");
            list.add("load3");
            list.add("load4");
            list.add("load5");
            list.add("load6");
            list.add("load7");
            list.add("load8");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(dataAdapter);
            }

            public void addItemsOnSpinner3() {

                spinner3 = (Spinner) findViewById(R.id.spinner3);
                List<String> list = new ArrayList<String>();
                list.add("Default");
                list.add("10");
                list.add("20");
                list.add("30");
                list.add("40");
                list.add("50");
                list.add("60");
                list.add("70");
                list.add("80");
                list.add("90");
                list.add("100");
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner3.setAdapter(dataAdapter);
                }

            public void addItemsOnSpinner4() {

                spinner4 = (Spinner) findViewById(R.id.spinner4);
                List<String> list = new ArrayList<String>();
                list.add("s1");
                list.add("s2");
                list.add("s3");
                list.add("s4");
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner4.setAdapter(dataAdapter);
                }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }   

            public class connectTask extends AsyncTask<String,String,TcpClient> {

                @Override
                protected TcpClient doInBackground(String... message) {

                    //we create a TcpClient object and
                    mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
                        @Override
                        //here the messageReceived method is implemented
                        public void messageReceived(String message) {
                            //this method calls the onProgressUpdate
                            publishProgress(message);
                        }
                    });
                    mTcpClient.run();

                    return null;
                }

            }
}

这是创建 tcp 客户端以与 WiFly 通信的类

    package com.example.homauto;

import android.util.Log;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;


public class TcpClient {

    public static final String SERVERIP = "192.168.1.5";
    public static final int SERVERPORT = 2000;
    // message to send to the server
    private String mServerMessage;
    // sends message received notifications
    private OnMessageReceived mMessageListener = null;
    // while this is true, the server will continue running
    private boolean mRun = false;
    // used to send messages
    private PrintWriter mBufferOut;
    // used to read messages from the server
    private BufferedReader mBufferIn;

    /**
     * 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 (mBufferOut != null && !mBufferOut.checkError()) {
            mBufferOut.println(message);
            mBufferOut.flush();
        }
    }

    /**
     * Close the connection and release the members
     */
    public void stopClient() {



        mRun = false;

        if (mBufferOut != null) {
            mBufferOut.flush();
            mBufferOut.close();
        }

        mMessageListener = null;
        mBufferIn = null;
        mBufferOut = null;
        mServerMessage = null;
    }

    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 {

                //sends the message to the server
                mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

                //receives the message which the server sends back
                mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));


                //in this while the client listens for the messages sent by the server
                while (mRun) {

                    mServerMessage = mBufferIn.readLine();

                    if (mServerMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                        mMessageListener.messageReceived(mServerMessage);
                    }

                }

                Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");

            } 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. The method messageReceived(String message) will must be implemented in the MyActivity
    //class at on asynckTask doInBackground
    public interface OnMessageReceived {
        public void messageReceived(String message);
    }
}

希望这对将来的人有所帮助。该代码适用于所有 tcp 服务器,而不仅仅是 WiFly。

于 2013-07-22T07:33:22.073 回答
0

如果问题出在基础设施(接入点的客户端)与 ad-hoc(点对点)无线网络模式上,我不会感到惊讶。

最简单的方法是,如果您有两个设备都可以连接的接入点。直接连接它们可能会有点困难,因为您必须让两者都在 ad-hoc 模式下工作,或者必须愿意玩接入点。

于 2010-11-04T23:33:17.767 回答
0

我们已经找到了解决这个问题的方法。我们将手机用作接入点,并将 WiFly 板连接到手机。我们通过将 WiFly 板连接到手机并将笔记本电脑连接到手机进行了测试。然后我们导航到 WiFly 板上的默认网页,我们能够看到该页面。这似乎运作良好,将解决我们的问题。但是,这会更快地耗尽手机电池。

于 2011-01-25T16:11:03.210 回答
0

有关不需要 root 的替代解决方案,请参阅

于 2012-10-15T11:01:50.180 回答