简体   繁体   中英

Establish communication between Android host and USB Device

i'm trying to implement a bidirectional communication between an android host and a USB device, of course i'm using the USB Host API provided by Google. The USB Device contains a firmware that echoes any character it receives. The user just inserts a character in a EditText and press a Button to send the character. Here is the code

public void sendData(View v){
    final String character = request.getText().toString() + "\n\r";
    log.setText(log.getText() + "\n" + "Sending...");
    if(character != null){          
        Thread t = new Thread(new Runnable() {              
                @Override
                public void run() {                     
                    byte[] array = character.getBytes();
                    Log.d("USB", "Sending Data...");
                    mDeviceConnection.bulkTransfer(mEndpointOut, array, array.length, 100);                 
                    ByteBuffer output_buffer = ByteBuffer.allocate(array.length);
                    Log.d("USB", "Creating Buffer");
                    UsbRequest req = new UsbRequest();
                    req.initialize(mDeviceConnection, mEndpointIn);
                    req.queue(output_buffer, array.length);                             
                    if(mDeviceConnection.requestWait() == req){
                        Log.d("USB", output_buffer.getChar(0) + "");                            
                        Message m = new Message();
                        m.obj = output_buffer.array();
                        handler.sendMessage(m);
                        output_buffer.clear();                      
                    } else{
                        Log.d("USB", "No USBRequest received");
                    }
                }
            });
        t.start();
    } 

}

The variable request is the EditText, log is a reference to a TextView so i can show some debbug information. First i send the character using the bulkTransfer method, then i use USBRequest to queue a request to read data on the in endpoint, aparently it send the character correctly, but whe i show the received character, it show some weird symbols, like chinese characters or a rhombus with a question symbol inside (almost always this is the response). I Want to know if the code i use for this communication is correct, and if it is, then why am i receiving that weird characters?

Thank you so much for you help

PD: Sorry for my english.

I am little bit uncleared about the question. But I suggest setting the baud rate correctly if you are using some minicom/putty kind of terminal application for showing the display. If that's not the problem, can you clarify when you say received character - does it mean received char on Android Host device or it means USB device ?

If you send byte array you'll get also array of bytes.

So use the method get(index of byte) instead of using getChar(index) !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM