Question

I'm trying to send hexadecimal data to connected USB HID device from my Nexus 7, but Android SDK method can work only with byte[] buffer.

How can I send hexadeсimal data that originates as decimal String values with 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, PC application with the device works good.

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

Was it helpful?

Solution

This depends on what the target of the message buffer will be.

Since you are getting decimal values from a String you can use the Integer.parseInt method with a radix of 10, then cast to a 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 simply want to send the data over the bulk pipe then you would send it as follows:

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

Control requests typically target some function on the USB device itself and is vendor defined. If you need to send the buffer as a control request you would send it as follows:

controlTransfer(USB_DIR_OUT, VENDOR_DEFINED_REQUEST, VENDOR_DEFINED_VALUE, USB_INTERFACE_INDEX, message, message.length, 1000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top