0

我正在使用 Freeduino(与 Arduino Uno 兼容)和运行 ICS(4)的三星 Galaxy Tab 10.1,并且我已成功从 Arduino 写入 Android,但我无法在 Arduino 草图中从 Android 读取.

这是 USB 附件的 Android 类:

package com.kegui.test;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.kegui.test.Scripto;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class USBAccess extends Activity implements Runnable, OnClickListener {
    private static final String ACTION_USB_PERMISSION = "com.google.android.DemoKit.action.USB_PERMISSION";

    protected static final String TAG = "KegUI";

    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;
    private boolean mPermissionRequestPending;

    private TextView debugtext = null;
        private Button button1 = null;
        private Button button2 = null;
        private boolean button2visible = false;



    UsbAccessory mAccessory;
    ParcelFileDescriptor mFileDescriptor;
    FileInputStream mInputStream;
    FileOutputStream mOutputStream;

    private static final int MESSAGE_BUTTON_PRESSED = 1;


    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i("KegApp", "***********************Received*************************");
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    if (intent.getBooleanExtra(
                            UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        openAccessory(accessory);
                    } else {
                        Log.d(TAG, "permission denied for accessory "
                                + accessory);
                    }
                    mPermissionRequestPending = false;
                }
            } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
                UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                if (accessory != null && accessory.equals(mAccessory)) {
                    closeAccessory();
                }
            }
        }
    };


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                    ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);

        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click

                new TempUpdateTask().execute("testing");
            }
        });
        registerReceiver(mUsbReceiver, filter);

        Log.d(TAG,"on Create'd");
    }

    @Override
    public void onResume() {
        super.onResume();

        if (mInputStream != null && mOutputStream != null) {
            return;
        }

        UsbAccessory[] accessories = mUsbManager.getAccessoryList();
        UsbAccessory accessory = (accessories == null ? null : accessories[0]);
        if (accessory != null) {
            if (mUsbManager.hasPermission(accessory)) {
                openAccessory(accessory);
            } else {
                synchronized (mUsbReceiver) {
                    if (!mPermissionRequestPending) {
                        mUsbManager.requestPermission(accessory,
                                mPermissionIntent);
                        mPermissionRequestPending = true;
                    }
                }
            }
        } else {
            Log.d(TAG, "mAccessory is null");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        closeAccessory();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbReceiver);

    }

    private void openAccessory(UsbAccessory accessory) {
        mFileDescriptor = mUsbManager.openAccessory(accessory);
        if (mFileDescriptor != null) {
            mAccessory = accessory;
            FileDescriptor fd = mFileDescriptor.getFileDescriptor();
            mInputStream = new FileInputStream(fd);
            mOutputStream = new FileOutputStream(fd);
            Thread thread = new Thread(null, this, "KegApp");
            thread.start();




            //enableControls(true);
        } else {
            Log.d(TAG, "accessory open fail");
        }
    }


    public void run() { 
        int ret = 0;
        byte[] buffer = new byte[16384];
        int i;
        Log.i("KegApp", "***********************in run*************************");
        while (ret >= 0) {
            try {
                ret = mInputStream.read(buffer); // this will be always positive, as long as the stream is not closed
            } catch (IOException e) {
                break;
            }

            i = 0;
            while (i < ret) {
                Message m = Message.obtain(messageHandler, MESSAGE_BUTTON_PRESSED);
                m.obj = buffer[i];
                messageHandler.sendMessage(m);
                i++;
            }

        }

    }



    private void closeAccessory() {
        //enableControls(false);

        try {
            if (mFileDescriptor != null) {
                mFileDescriptor.close();
            }
        } catch (IOException e) {
        } finally {
            mFileDescriptor = null;
            mAccessory = null;
        }
    }

    public void sendCommand(FileOutputStream mStream) { 
        BufferedOutputStream bo = new BufferedOutputStream(mStream);

    //  if (mStream != null && message.length > 0) {
            try {
                Log.i("KegApp", "***********************sending command now*************************");
                bo.write(1); //message, 0, 3);
            } catch (IOException e) {
                Log.e(TAG, "write failed", e);
            }
    //  }
    }

    @Override
    public void onClick(View v) {
        // Send some message to Arduino board, e.g. "13"
        Log.e(TAG, "write failed");
    }

    // Instantiating the Handler associated with the main thread.
      private Handler messageHandler = new Handler() {

          @Override
          public void handleMessage(Message msg) { 
              Log.i("KegApp", "***********************message handler before " + msg.what +  "************************");
                try {  
                    String load = msg.obj.toString();
                    Log.i("KegApp", "***********************in message handler*************************");
                    /*
                    if (button2visible==false) {
                        debugtext.setText("Received message: "+String.valueOf(load));
                        button2.setVisibility(View.VISIBLE);
                        button2visible = true;
                    } else {
                        debugtext.setText("");
                        button2.setVisibility(View.GONE);
                        button2visible = false;
                    }
                    */
                    new TempUpdateTask().execute(load);

                } catch (Exception e) {
                    Log.e(TAG, "message failed", e);
                }
          }

      };

// UpdateData Asynchronously sends the value received from ADK Main Board.
// This is triggered by onReceive()
class TempUpdateTask extends AsyncTask<String, String, String> {

    // Called to initiate the background activity
    protected String doInBackground(String... sensorValue) {
            try {
                Log.i("KegApp", "***********************calling sendcommand*********************");
                sendCommand(mOutputStream);

                Log.i("KegApp", "*********************incoming-sensorValue*********************" );
                ArduinoMessage arduinoMessage = new ArduinoMessage(sensorValue[0]);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String returnString = String.valueOf(sensorValue[0]);//String.valueOf(sensorValue[0]) + " F";
            publishProgress(String.valueOf(sensorValue[0]));
            return (returnString); // This goes to result

    }

    // Called when there's a status to be updated
    @Override
    protected void onProgressUpdate(String... values) {
        // Init TextView Widget to display ADC sensor value in numeric.
        TextView tvAdcvalue = (TextView) findViewById(R.id.tvTemp);
        tvAdcvalue.setText(String.valueOf(values[0]));

        // Not used in this case
    }

    // Called once the background activity has completed
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        closeAccessory();
    }

}
}

这是我的 Arduino 草图:

#include <FHB.h>
#include <Max3421e.h>
#include <Max3421e_constants.h>
#include <Max_LCD.h>
#include <Usb.h>


//FREEDUINO ONLY------------------------------------

//END FREEDUINO ONLY------------------------------------
int pin_light_sensor_1=A0;
int pin_output_sensor=9;
int currLightLevel=0;
//int prevLightLevel=0;
//int lightStableRange=90;
int delayTime=3000;
int loopCtr=0;
char cSTX=char(2);
char cSOH=char(1);
char cEOT=char(4);
char cGS=char(29);
char cRS=char(30);
char cCR=char(13);
char cLF=char(10);

//FREEDUINO ONLY------------------------------------
AndroidAccessory acc("Company Inc.",
             "Kegbot5K datawriter",
             "Kegbot 5000 data writer",
             "1.0",
             "http://companyinc.com",
             "0000000012345678");
//END FREEDUINO ONLY------------------------------------
void setup()
{
  pinMode(pin_light_sensor_1, INPUT);
  pinMode(pin_output_sensor, OUTPUT);
  Serial.begin(57600);
//FREEDUINO ONLY------------------------------------  
  Serial.println("pre-power");
  acc.powerOn();
  Serial.println("post-power");
//END FREEDUINO ONLY------------------------------------
}

void loop()
{
byte msg[3];
if (acc.isConnected()) {
  currLightLevel = analogRead(pin_light_sensor_1);

     //sysPrint(delayTime*loopCtr);
     //sysPrint(",");
     //sysPrint(currLightLevel);
     writeDataMessage("LGH01", currLightLevel);

 }

 delay(1000);
 if (acc.isConnected()) {
    int len = acc.read(msg, sizeof(msg), 1);
    Serial.println(len);
      if (len > 0){
     for (int index=0; index < len; index++){
        digitalWrite(pin_output_sensor, 1);
                delay(500);
        digitalWrite(pin_output_sensor, 0);
     }
      }
 }
  loopCtr++;
  delay(delayTime);
}

void writeHeader(String msgType)
{
  sysPrint(cSTX);
  sysPrint(cSTX);
  sysPrint(cSOH);
  sysPrint(msgType);
  sysPrint(cGS);
}

void writeFooter()
  {
  sysPrint(cEOT);
  sysPrintLn(cEOT);
  }

void writeDataMessage(String sensorID, int value)
  {
  writeHeader("DATA");
  sysPrint(sensorID);
  sysPrint(cRS);
  sysPrint(value);
  writeFooter();
  }

void writeAlarmMessage(String sensorID, String message)
//Do we need to enforce the 5 char sensorID here? I don't think so...
  {
  writeHeader("ALRM");
  sysPrint(sensorID);
  sysPrint(cRS);
  sysPrint(message);
  writeFooter();
  }

void sysPrint(String whatToWrite)
  {
    int len=whatToWrite.length();
  char str[len];
  whatToWrite.toCharArray(str, len);
  acc.write((void *)str, len);
 // acc.write(&whatToWrite, whatToWrite.length());
  }
void sysPrint(char whatToWrite)
  {
  acc.write((void *)whatToWrite, 1);
 // acc.write(&whatToWrite, 1);
  }
void sysPrint(int whatToWrite)
  {
  acc.write((void *)&whatToWrite, 1);
//  acc.write(&whatToWrite, 1);
  }

void sysPrintLn(String whatToWrite)
  {
  int len=whatToWrite.length();
  char str[len];
  whatToWrite.toCharArray(str, len);
  acc.write((void *)str, len);
  acc.write((void *)&cCR, 1);
  acc.write((void *)&cLF, 1);
 // acc.write(&whatToWrite, whatToWrite.length());
 // acc.write(&cCR, 1);
 // acc.write(&cLF, 1);
  }

void sysPrintLn(char whatToWrite)
  {
  acc.write((void *)whatToWrite, 1);
   acc.write((void *)&cCR, 1);
  acc.write((void *)&cLF, 1);
 // acc.write(&whatToWrite, 1);
 // acc.write(&cCR, 1);
 // acc.write(&cLF, 1);
  }

Android 中的 sendCommand 函数正在记录它已发送。acc.read 函数将长度打印到串行输出为 -1,就像没有输入一样。说到这一点,Android 日志没有显示错误,所以我认为这可能是 Arduino 的问题。

我的清单有一个注册设备的意图过滤器,尽管权限可能与它有关。

 <intent-filter>
        <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    </intent-filter>

    <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
        android:resource="@xml/accessory_filter" />
        </activity>

提前感谢您的任何想法,萨拉

4

2 回答 2

1

我开发了一种遥控直升机,可以通过使用蓝牙棒从 android 向位于直升机顶部的 arduino 发送消息来使用 android 进行控制,我还没有将 USB 用于任何项目,但是当您向 arduino 发送消息时正在使用串行通信天气连接抛出 USB 或蓝牙我建议简单地使用 Serial.read 并且您发送到 arduino 的每条消息都以一些符​​号“#”结尾,例如只是为了分隔消息并逐个字符地获取消息这是代码从串行端口读取以“#”结尾的完整消息:

    String getMessage()
    {
      String msg=""; //the message starts empty
      byte ch; // the character that you use to construct the Message 
      byte d='#';// the separating symbol 

      if(Serial.available())// checks if there is a new message;
      {
        while(Serial.available() && Serial.peek()!=d)// while the message did not finish
        {
          ch=Serial.read();// get the character
          msg+=(char)ch;//add the character to the message
          delay(1);//wait for the next character
        }
      ch=Serial.read();// pop the '#' from the buffer
      if(ch==d) // id finished
      return msg;
      else
      return "NA";
      }
    else
    return "NA"; // return "NA" if no message;
}

此函数检查缓冲区中是否有任何消息,如果没有则返回“NA”。如果这没有帮助,请告知。

于 2012-04-26T16:44:08.337 回答
0

弄清楚出了什么问题 - sendCommand 是从异步 TempTask 执行的,因此它没有传递对 FileOutputStream 的有效引用。我可以从主线程执行 sendCommand onClick 并在 Arduino 上接收就好了。

于 2012-04-25T14:28:45.000 回答