Arduino Android USB connection

这一生的挚爱 提交于 2019-12-01 10:37:59

The project you reference from http://android.serverbox.ch/?p=549 in comments will only work with the more recent Arduino boards such as the Uno (or with something else which uses the CDC-ACM serial-over-USB scheme which they implement).

The older Arduino boards such as your duemilanove use an FTDI USB-serial chip, which would require different USB operations.

You may be able to find something somewhere about interfacing the FT232 family of USB-serial converters with Android's USB host api (the FTDI parts are widely used in embedded systems, so it's not out of the question you'll find something), or you can get an Uno board to try the code you already have.

I know it's been a while but anyway, I made a short library to do this. This is how you use it :

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();
}

And the code on Arduino side could be something like this :

void setup() {
    Serial.begin(9600);
}

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

You can check the github page if you are interested.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!