Arduino Android USB connection



我正在使用Arduino Duemilanove和Nexus 7。我已成功检测到 Arduino 板并显示供应商 ID 和产品 ID。我正在尝试将数据从平板电脑传输到Arduino板,并尝试使板上的LED闪烁。安卓的代码如下。

主要活动.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        UsbDeviceConnection connection;
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        UsbDevice device = null;
        while(deviceIterator.hasNext()){
            device = deviceIterator.next();
            String s = device.getDeviceName();
            int pid = device.getProductId();
            int did = device.getDeviceId();
            int vid = device.getVendorId();
            TextView tv = (TextView) findViewById(R.id.textview);
            tv.setText(s+"n"+Integer.toString(pid)+"n"+Integer.toString(did));
        }
        connection = manager.openDevice(device);
        DataTransfer dt = new DataTransfer(device,connection);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

DataTrasfer.java

public class DataTransfer extends Thread {
    UsbDevice device;
    UsbDeviceConnection connection;
    byte data = 1;
    public DataTransfer(UsbDevice device, UsbDeviceConnection connection) {
        // TODO: Auto-generated constructor stub
        device = this.device;
        connection = this.connection;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        UsbInterface usbIf = device.getInterface(0);
        UsbEndpoint end1 = usbIf.getEndpoint(0);
        //UsbRequest ureq = connection.requestWait();
        final Object[] sSendLock = new Object[]{};
        boolean mStop = false;
        byte mData = 0x00;
        if(!connection.claimInterface(usbIf, true))
        {
            return;
        }
        connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
        connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
                0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
        connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); //Baudrate 9600
        UsbEndpoint epIN = null;
        UsbEndpoint epOUT = null;
        for (int i = 0; i < usbIf.getEndpointCount(); i++) {
            if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                    epIN = usbIf.getEndpoint(i);
                else
                    epOUT = usbIf.getEndpoint(i);
            }
        }
        for (;;) { // This is the main loop for transferring
            synchronized (sSendLock) { //OK, there should be a OUT queue, no guarantee that the byte is sent actually.
                try {
                    sSendLock.wait();
                }
                catch (InterruptedException e) {
                    if (mStop) {
                        return;
                    }
                    e.printStackTrace();
                }
                connection.bulkTransfer(epOUT,new byte[] {data}, 1, 0);
            }
        }
    }
}

我的Arduino代码是:

int incomingByte = 0; // For incoming serial data
void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600); // Opens serial port, sets data rate to 9600 bps.
}
void loop() {
    // Send data only when you receive data:
    if (Serial.available() > 0) {
        // Read the incoming byte:
        incomingByte = Serial.read();
        digitalWrite(13, HIGH);   // Set the LED on
        delay(1000);              // Wait for a second
        digitalWrite(13, LOW);    // Set the LED off
        // Say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte, DEC);
    }
}

问题是Arduino板上没有LED发光。我不确定问题出在哪里,因为我刚刚开始在这个领域工作。

您在评论中引用 http://android.serverbox.ch/?p=549 的项目只能与最新的Arduino板(如Uno)一起使用(或使用他们实现的CDC-ACM串行USB方案的其他东西)。

较旧的Arduino板(例如duemilanove)使用FTDI USB串行芯片,这需要不同的USB操作。

您也许可以在某处找到有关将FT232系列USB串行转换器与Android的USB主机api连接的东西(FTDI部件广泛用于嵌入式系统,因此您找到一些东西并非不可能),或者您可以获得Uno板来尝试您已经拥有的代码。

我知道

已经有一段时间了,但无论如何,我做了一个简短的库来做到这一点。这是你使用它的方式:

Arduino arduino = new Arduino(Context);
@Override
protected void onStart() {
    super.onStart();
    arduino.setArduinoListener(new ArduinoListener() {
        @Override
        public void onArduinoAttached(UsbDevice device) {
            // arduino plugged in
            arduino.open(device);
        }
        @Override
        public void onArduinoDetached() {
            // arduino unplugged from phone
        }
        @Override
        public void onArduinoMessage(byte[] bytes) {
            String message = new String(bytes);
            // new message received from arduino
        }
        @Override
        public void onArduinoOpened() {
            // you can start the communication
            String str = "Hello Arduino !";
            arduino.sendMessage(str.getBytes());
        }
    });
}
@Override
protected void onDestroy() {
    super.onDestroy();
    arduino.unsetArduinoListener();
    arduino.close();
}

Arduino端的代码可能是这样的:

void setup() {
    Serial.begin(9600);
}
void loop() {
    if(Serial.available()){
        char c = Serial.read();
        Serial.print(c);
    }
}

如果您有兴趣,可以查看github页面。

最新更新