使用wifi Shield将arduino连接到android

时间:2014-06-16 15:50:24

标签: android arduino android-wifi

我正在尝试使用Wifi通过Common Lan连接将arduino Uno连接到Android设备,但以下代码没有给我任何来自Arduino的响应。 Arduino与桌面检查代码配合得很好。这是我点击切换按钮时代码的android部分

//WIFI socket code
    ToggleButton toggle = (ToggleButton) findViewById(R.id.wifiTButton);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                // wifi thread code

                final Runnable r = new Runnable()
                {
                    public void run() 
                    {

                     Socket socket = null;
                     DataOutputStream dataOutputStream = null;
                     DataInputStream dataInputStream = null;
                    textOut = (TextView) findViewById(R.id.textOut);
                     textIn=(EditText)  findViewById(R.id.textIn);
                     try {
                      socket = new Socket("10.0.0.101", 7);
                     dataOutputStream = new DataOutputStream(socket.getOutputStream());
                      dataInputStream = new DataInputStream(socket.getInputStream());
                      dataOutputStream.writeByte(111111);
                      //dataOutputStream.writeUTF("111111".toString() );//textOut.getText().toString());
                      textIn.setText(dataInputStream.readByte());
                     // textIn.setText(dataInputStream.readUTF());
                        Log.d(TAG, " why is not working");

                    } catch (UnknownHostException e) {
                        Log.d(TAG, "Unknown Host");
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "Input Output Exception");
                        e.printStackTrace();
                    }                   
                     finally{
                      if (socket != null){
                       try {
                        socket.close();
                       } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                       }
                      }

                      if (dataOutputStream != null){
                       try {
                        dataOutputStream.close();
                       } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                       }
                      }

                      if (dataInputStream != null){
                       try {
                        dataInputStream.close();
                       } catch (IOException e) {
                        e.printStackTrace();
                       }
                      }
                     }

                      handler.postDelayed(this, 1000);
                    }
                };

              handler.postDelayed(r, 1000);


             } 

        }
    });

1 个答案:

答案 0 :(得分:1)

我能够解决问题。它使用命令按钮而不是切换按钮并正确使用线程语法运行良好。 这是一个示例应用程序mainActivity代码。

package com.example.arduinoandroid;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 import android.widget.Toast;
    public class MainActivity extends Activity {
Handler handler;
String TAG = "main Activity";
Button bt1;
TextView textOut;
EditText editIn;
static byte abc;
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;

class Task implements Runnable {

    @Override
    public void run() {

        try {
            socket = new Socket("10.0.0.101", 7);
            dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());

            dataOutputStream.writeByte(111);

            // dataOutputStream.writeUTF("111111".toString()
            // );//textOut.getText().toString());
            abc = dataInputStream.readByte();
            Toast.makeText(getBaseContext(), abc, Toast.LENGTH_SHORT)
                    .show();
            // textIn.setText(dataInputStream.readUTF());
            Log.d(TAG, " why is not working");

        } catch (UnknownHostException e) {
            Log.d(TAG, "Unknown Host");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d(TAG, "Input Output Exception");
            e.printStackTrace();
        }

        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                textOut.setText("working ae");
            }

        });

    }

}

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

    handler = new Handler();
    // WIFI socket code
    bt1 = (Button) findViewById(R.id.chkButton1);
    textOut = (TextView) findViewById(R.id.textOut);
    editIn = (EditText) findViewById(R.id.editIn);
    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new Thread(new Task()).start();

        }

    });

}

 }

和xml部分的代码是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >

<Button
    android:id="@+id/chkButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="140dp"
    android:text="Check wifi" />

<TextView
    android:id="@+id/textOut"
    android:layout_width="wrap_content"
    android:layout_height="39dp"
    android:layout_gravity="bottom|right"
    android:text="Output" />

<EditText
    android:id="@+id/editIn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:ems="10"
     android:text="111111"/>
  </LinearLayout>

请在清单文件中包含以下权限

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />