Android HID USB, how to send hex data with bulkTransfer or controlTransfer?

I am trying to send hex data to a connected USB HID device from my Nexus 7, but the Android SDK method can only work with the buffer [].

How can I send hexadecimal data that starts as decimal String values ​​using bulkTransfer or controlTransfer?

message[0]= 0; message[1]= 166; message[2]= 2; message[3]= 252; message[4]= 255; 

SDK methods:

 bulkTransfer(UsbEndpoint endpoint, byte[] buffer, int length, int timeout) controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout) 

like this: http://pure-basic.narod.ru/article/pickit2.html , the PC application with the device works well.

 OutBuffer(0)=0 OutBuffer(1)=$A6 ; EXECUTE_SCRIPT OutBuffer(2)=2 OutBuffer(3)=$FC ; _VDD_GND_OFF OutBuffer(4)=$FF ; _VDD_ON 

UPDATE - ANSWER

 private void sendData() { //byte b = (byte) 129; // (byte) 0x81 Also work int status = connection.bulkTransfer(endPointWrite, toByte(129), 1, 250); } private static byte toByte(int c) { return (byte) (c <= 0x7f ? c : ((c % 0x80) - 0x80)); } // for received data from USB HID device private static int toInt(byte b) { return (int) b & 0xFF; } 

My application on Google play - USB HID TERMINAL

+4
source share
1 answer

It depends on what the purpose of the message buffer will be.

Since you get decimal values ​​from String, you can use the Integer.parseInt method with a radius of 10, and then apply to byte :

 byte message[] = new byte[] { (byte)java.lang.Integer.parseInt("0", 10), (byte)java.lang.Integer.parseInt("166", 10), (byte)java.lang.Integer.parseInt("2", 10), (byte)java.lang.Integer.parseInt("252", 10), (byte)java.lang.Integer.parseInt("255", 10) }; 

If you just want to send data on the main channel, you should send it as follows:

 bulkTransfer(outEndpoint, message, message.length, 1000); 

Audit requests typically target some features on the USB device itself and are determined by the vendor. If you need to send a buffer as a management request, you must send it as follows:

 controlTransfer(USB_DIR_OUT, VENDOR_DEFINED_REQUEST, VENDOR_DEFINED_VALUE, USB_INTERFACE_INDEX, message, message.length, 1000); 
+1
source

Source: https://habr.com/ru/post/1497000/


All Articles