2

I am using an Arduino Duemilanove and Nexus 7. I have successfully detected the Arduino board and displayed the vendor id and product id. I am trying to transfer data from my tablet to the Arduino board and trying to blink the LED on the board. The code for Android is as follows.

Main Activity.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);
            }
        }
    }
}

And my Arduino code is:

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

The problem is that there is no LED glowing on the Arduino board. I am not sure where the problem lies as I have just started working in this area.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sohil
  • 223
  • 2
  • 4
  • 13
  • Are you sure those are the correct USB transfers to send data to the FTDI chip on that Arduino? Talking to that device is substantially different from talking to the CDC-ACM firmware in the Uno, so depending where you got that code it may not be suitable. – Chris Stratton Feb 02 '13 at 13:44
  • i am not sure about that [link](android.serverbox.ch/?p=549) this is from where i reffered.. – sohil Feb 02 '13 at 14:15
  • i am not much of hardware guy i am quite unaware of these terms – sohil Feb 02 '13 at 14:20
  • Hey Sohil would you please share the code for "I have successfully detected the Arduino board and displayed the vendor id and product id." ?, I am looking for if is there any way to check that Arduino is connected with and android phone and android phone can query the Arduino's device token ? – βhargavḯ May 13 '15 at 09:16

2 Answers2

1

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.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • [link](http://stackoverflow.com/questions/9768296/communicating-with-arduino-from-android) there's a bit of misunderstanding according to what this guy has to say. according to him if its ftdi i dont have to worry about anything. i don't understand wad should i do. please if possible provide the links that i should refer to – sohil Feb 06 '13 at 07:38
  • i am getting an error(null pointer exception) in usbManager.openDevice(deviceobject). – sohil Feb 06 '13 at 09:40
  • @sohil - you are misreading that link, which is to a rather low quality answer anyway. The point there is that there's nothing to worry about on the Arduino side - though there wouldn't be on any Uno either. **But on the Android side, you are still apparently trying to use interface code for a different Arduino than you have**. You won't get anywhere until you start using code appropriate to the hardware, or hardware appropriate to the code. – Chris Stratton Feb 06 '13 at 15:52
  • i donno i am somehow not convinced bcoz i use a freeduino board which is not exactly an arduino. but when i write sketch for it i have to select the duemilanove board to code. [link](http://stackoverflow.com/questions/14168947/usb-host-for-android/14171073#comment19651570_14171073) this is the qns i posted and that is he board dat i am using. For all the research i have done so far. I have never heard of nethn u have said above. I m not saying u r wrong but i m not understanding what i should be doing ahead. coz fe friends of mine who r working using that board say there shouldnt be a prob. – sohil Feb 07 '13 at 16:56
  • @sohil - you are not going to make any progress until you face the facts of the situation: you have the wrong driver code for your board's USB interface chip. – Chris Stratton Feb 07 '13 at 19:17
  • alryt i vil try ot get a uno board from my frd as i am not willing to invest any more widout confirmation. after dat i vil contact you again – sohil Feb 09 '13 at 05:11
  • Dude thanks a lot.. you were right.. below is the code snippet for others – sohil Feb 18 '13 at 10:40
0

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.

Omar Aflak
  • 2,918
  • 21
  • 39