I'm trying to make a communication between Android and a controller. I would like to ask how to configure the message, since I have seen different ways, but I can not know why each way. I want to send the message from Android to the controller, in 32 bytes. I have this code:
private void sendData(){
int baudRate = 115200;
byte stopBitsByte = 1;
byte parityBitesByte = 0;
byte dataBits = 32;
byte[] msg = {
(byte) (baudRate & 0xff),
(byte) ((baudRate >> 8) & 0xff),
(byte) ((baudRate >> 16) & 0xff),
(byte) ((baudRate >> 24) & 0xff),
stopBitsByte,
parityBitesByte,
(byte) dataBits
};
deviceConnection.controlTransfer(UsbConstants.USB_TYPE_CLASS | 0x01, 0x20, 0, 0, msg, msg.length,5000);
deviceConnection.controlTransfer(UsbConstants.USB_TYPE_CLASS | 0x01, 0x22, 0x1, 0, null, 0, 0);
}
Another thing that I have very clear is how a message is received from the controller to Android. For this I have this code:
private void receiveData(){
int bufferMaxLength=epIN.getMaxPacketSize();
ByteBuffer mBuffer = ByteBuffer.allocate(bufferMaxLength);
UsbRequest inRequest = new UsbRequest();
inRequest.initialize(deviceConnection, epIN);
while(inRequest.queue(mBuffer, bufferMaxLength) == true) {
deviceConnection.requestWait();
}
}
PS: Is the first time I use UsbDevice, UsbManager, USBInterface, UsbEndpoint, UsbDeviceConnection, UsbRequest or UsbConstants.
PS2: The codes are provided to send and receive a message from Android Arduino and vice versa.