Как сделать асинхронный запрос на связь с USB hid устройством в android

Я пытаюсь создать приложение, которое может обмениваться данными с USB-микроконтроллером msp430 USB (класс HID), поэтому я пытаюсь отправить букву T для recupperer текущей даты в микроконтроллере (асинхронный). когда я вызываю функцию "getLokerTime", приложение вылетает. Я думаю, что это из-за "if (connection.requestWait () == запрос)", потому что, когда я добавляю предоставленные комментарии, приложение не падает.

вы не могли бы мне помочь?

void getLokerTime (соединение UsbDeviceConnection){

    int bufferMaxLength=endPointWrite.getMaxPacketSize();

    ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
    UsbRequest request = new UsbRequest(); // create an URB


    request.initialize(connection, endPointWrite);

    buffer.putChar('T');

    // queue the outbound request
    boolean retval = request.queue(buffer, 1); 

    Toast.makeText(getApplicationContext(), " envoi de la donnée " + retval  , Toast.LENGTH_SHORT).show();
    if (connection.requestWait() == request) { 
        //if(retval == true){
             // wait for confirmation (request was sent)
             UsbRequest inRequest = new UsbRequest(); 
             // URB for the incoming data
             inRequest.initialize(connection, endPointRead); 
             // the direction is dictated by this initialisation to the incoming endpoint.
             if(inRequest.queue(buffer, bufferMaxLength) == true){
                 connection.requestWait(); 
                  // wait for this request to be completed
                  // at this point buffer contains the data received
                 byte[] dst = new byte[8];
                 buffer.get(dst);
                 buffer.clear();
                 String contenu;


                //byte[] data = buffer.array();
                //String str = new String( data);


                 try {

                    contenu = new String(dst , "UTF-8");
                    Toast.makeText(getApplicationContext(), " le contenu du buffer : " +  contenu , Toast.LENGTH_SHORT).show();
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }




             }
        }

}

0 ответов

Другие вопросы по тегам