Android配件和Arduino olimexino 32u4

时间:2014-07-12 16:28:42

标签: android arduino accessory

我正在尝试为Android和Ardunio控制器之间的通信编写应用程序,但它不起作用。当我插入两个设备时,我的应用程序必须启动,但它不会启动。我不知道问题出在哪里。我设置了Intent过滤器和Broadcast for Attached。

抱歉我的英文。

这是我的主要信息:

<?xml version="1.0" encoding="utf-8"?>

<uses-feature android:name="android.hardware.usb.accessory" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".BordActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
            android:resource="@xml/accessory_filter" />
    </activity>
    <activity
        android:name=".MapActivity"
        android:label="@string/title_activity_map"
        android:screenOrientation="landscape" >
    </activity>
</application>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

这是我的附件应用代码:

 private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        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 =   intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
            if (accessory != null && accessory.equals(mAccessory)) {
                closeAccessory();
            }
        }
    }
};
 @Override
public Object onRetainNonConfigurationInstance() {
    if (mAccessory != null) {
        return mAccessory;
    } else {
        return super.onRetainNonConfigurationInstance();
    }
}
private void setupAccessory(){
    //Reference to USB System Service
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    //Ask for USB Permission when an Accessory connects
    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);
    // Register the Receiver
    registerReceiver(mUsbReceiver, filter);
    if (getLastNonConfigurationInstance() != null) {
        mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
        openAccessory(mAccessory);
    }

}
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, "ChargeKit");
        thread.start();
        Log.d(TAG, "accessory opened");
    } else {
        Log.d(TAG, "accessory open fail");
    }
}
private void closeAccessory() {

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

@Override
public void run() {
    int ret = 0;
    byte[] buffer = new byte[16384]; //ByteArray Buffer with 16384 space.
    int i;
    while (true) {
        try {
            ret = mInputStream.read(buffer);    //Read from mInputstream and stored in Buffer
        } catch (IOException e) {
            break;
        }
        i = 0;
        while (i < ret) {
            int len = ret - i;
            if(len >=1){
                Message m = Message.obtain(mHandler);
                int value = (int) buffer[i];
                m.obj = new ValueMsg(value);
                mHandler.sendMessage(m);
            }
            i += 1; // number of bytes sent from arduino
        }

    }

}

Handler mHandler = new Handler()
{
    public void handleMessage( Message msg){
        final ValueMsg  t = (ValueMsg) msg.obj;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                txt_range.setText(""+t.getReading());
                txt_consumption.setText(""+t.getReading());

            }
        });
    }


};

@Override
public void onDestroy() {
    unregisterReceiver(mUsbReceiver);
    super.onDestroy();
}
public class ValueMsg{
    private int reading;

    public ValueMsg(int reading){
        this.reading = reading;
    }
    public int getReading(){
        return reading;
    }
}

这是一个Arduino文本程序:

#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>

         AndroidAccessory acc("FH-Frankfurt",
         "ChargeKit",
         "1.0");
 void setup()
 {
 Serial.begin(115200);
 acc.begin();
}

void loop()
{
 byte msg[1]; // one byte
 int value=10; // value to send,we'll increment and decrement this variable
if (acc.isConnected()) 
{
// is connected
while(value>0)
{
  // count down
  msg[0] = value;
  acc.write(msg, 1);
  delay(1000);
  value-=1;
  }

  while(value<=10)
  {
    // count up+
    msg[0] = value;
    acc.write(msg, 1);
    delay(1000);
    value+=1;
  }
}

}

accessory_filter:

<?xml version="1.0" encoding="utf-8"?>

<resources>
<usb-accessory model="ChargeKit" manufacturer="FH-Frankfurt" version="1.0"/>
</resources>

1 个答案:

答案 0 :(得分:0)

尝试分离

的意图过滤器
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />

<category android:name="android.intent.category.LAUNCHER" />

如同,请尝试:

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