开发者

Connect an Android phone to the WiFly board

My team and I are trying to connect an Android phone to the WiFly board. We are having trouble getting this to connect. Is it that certain phones that do not support this or do support this functionality and what must we do to be able to perform this connection?

We have tried a few things, and it is 开发者_StackOverflow社区still not working. If you need further information let me know.


I've had a project based on WiFly module and have successfully developed an app that makes a tcp as well as udp connections with the module over the wifi network. No, i didn't need to create my phone as an access point. I can communicate from my phone to WiFly with a wireless router as access point. I could also make it enter command mode so that I can edit its configuration. If you are still searching for a viable solution, I would be happy to provide you with my code.

EDIT: Below is the entire code for the app i made. It has been used to connect to WiFly in a Home Automation project.

The MainActivity

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

            }
}

And this is the class the creates a tcp client to communicate with 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);
    }
}

Hope this help somebody in future. The code is applicable to all tcp servers and not just WiFly in particular.


I wouldn't be surprised if the problem is with infrastructure (clients of an access point) vs. ad-hoc (peer-to-peer) wireless networking modes.

The easiest would be if you have an access point that both devices can connect to. Directly connecting them may be a bit harder, as you have to either get both working in ad-hoc mode or one has to be willing to play access point.


We have found a solution for this issue. We are making the phone act as an access point and having the WiFly board connect to the phone. We tested this by having the WiFly board connected to the phone and had a laptop connect to the phone. We then navigated to the default webpage that is on the WiFly board and we were able to see the page. This seems to be working well and will solve our problem. However, this will drain the phones battery quicker.


For an alternate solution not requiring root, see this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜