2012-04-24 49 views
0

我使用三星Galaxy Tab 10.1运行ICS(4)的Freeduino(Arduino Uno兼容),并且我已经成功地从Arduino写入Android,但是我一直无法从Android中读取Arduino素描。ADK AndroidAccessory在Arduino上读取函数应该得到一些东西,根据DemoKit草图?

下面是USB附件安卓类:

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", 
      "0000000"); 
//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过滤器,虽然权限可能与它有关。

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

感谢您的任何想法, 萨拉

+0

如果有人想知道,我植根了我的标签,并且正在运行CyanogenMod 9版本的ICS,因为姜饼似乎有USB附件问题。 – saranicole 2012-04-24 21:22:25

回答

0

想通了什么错误 - sendCommand从异步TempTask执行所以它不会传递给FileOutputStream中一个有效的参考。我可以从主线程执行sendCommand onClick并在Arduino上收到。

1

我开发了一个遥控直升机,可以使用android通过发送消息从android到位于直升机顶部的arduino使用蓝牙棒来控制,我没有使用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”。如果没有帮助请通知我 。

相关问题