Tuesday, February 4, 2014

Android USB Host Mode, step-by-step

When your Android-powered device is in USB host mode, it acts as the USB host, powers the bus, and enumerates connected USB devices. USB host mode is supported in Android 3.1 and higher.

To use Android devices as Host, a USB OTG (On-The-Go) cable is needed, to be connected to Android side.

I'm do some exercise on Android USB Host Mode, here is the list of the related posts.



8 comments:

  1. Muy buenos tutoriales, te queria hacer una pregunta:
    Estoy realizando un proyecto con arduino y android y tengo un problema, resulta que cuando conecto el arduino a la tablet esta lo reconoce pero si desconecto el arduino y conecto otra vez ya no lo reconoce, como logro que reconozca automaticamente el arduino sin salirme de la aplicacion.

    Saludos desde colombia

    ReplyDelete
  2. Hello Andr.oid Eric,
    You have a nice examples!!
    Have you considered an application to pass data from the arduino to smartphone?
    Thank!

    ReplyDelete
  3. Hello Javier Redondo,

    I have tried. But unfortunately, I fail to program Android to receive data in USB Host mode.

    May be I will try later.

    ReplyDelete
  4. hey, blogger thank u so much for posting these fantastic posts. Currently I'm trying to read out Arduino serial data to Android phone as a monitor. I used part of ur code, and can read out some data, but the usb connection is very unstable. It shows connect and disconnect all the time. I just put a new thread to get the data and use message to send the data to handler to update the ui,
    like this,
    public void run(){


    byte[] buffer=new byte[4096];
    final int ret=usbDeviceConnection.bulkTransfer(endpointIn, buffer, buffer.length, 0);
    if(ret>0){
    byte[] buffer2=new byte[ret];
    System.arraycopy(buffer,0,buffer2,0,ret);


    String data = new String(buffer2);

    //String data=buffer2.toString();
    Message m=new Message();
    m.what=1;
    Bundle b=new Bundle();
    b.putString("data", data);
    b.putInt("length", ret);
    //b.putByteArray("abc", buffer2);
    m.setData(b);
    mHandler.sendMessage(m);
    mHandler.postDelayed(this, 200);
    } }
    Handler mHandler=new Handler(){

    public void handleMessage(Message msg){

    if(msg.what==1){

    Bundle b=msg.getData();
    String data="";
    //data=data+b.getString("data");
    //int length=b.getInt("length");
    tv.append(data+b.getString("data"));
    //tv.append("\n"+length);
    }
    }
    };

    ReplyDelete
  5. hello Judy,

    I'm not sure your case. In my trial experience, the problem of connection unstable may be caused by power consumption. Maybe you have to check how much current needed for your Arduino device.

    Or you can try on different device, and also charge-up the device before test.

    ReplyDelete
  6. hello sir,

    I have to develop an android application that accepts a weighing machine that is connected to the android device via usb port, and the weight of the object must be displayed on the screen, I followed your tutorial i am able to establish connection with usb host but not able to read data from. Please help in this context.

    thankyou

    ReplyDelete
  7. hello sir,

    I have to make an android app that reads the data from a weighing machine that is connected to the android device via usb port and display the weight of the object on the device screen. after following your tutorials I am able to establish connection with the usb device , but still not able to read the data from the usb host. please help me in this context.

    thankyou

    ReplyDelete