如何将数据从android发送到主机USB设备

时间:2014-07-08 21:02:33

标签: java android usb host infrared

AnyOne可以帮我解决这个问题吗?

我想从android传输到USB。我有一些技术问题试图从我的Android代码发送数据到外部USB设备。我有供应商和其他信息;

这是我的设备供应商信息:

<resources>
    <usb-device
        class="0"
        product-id="30209"
        protocol="0"
        subclass="0"
        vendor-id="5263" />
</resources>



String s = "55 AA 81 8 F7 A F5 0";
                    byte [] bytes = s.getBytes();
                    connection.bulkTransfer(endpoint, bytes, bytes.length, 0); //do in another thread

这是我的代码:

private void doYourOpenUsbDevice(final UsbDevice usbDevice) {
        // now follow line will NOT show: User has not given permission to
        // device UsbDevice
         final UsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);
        // here is your device-
        if(usbDevice.getDeviceId()==2002)
        {


            Thread thread = new Thread(new Runnable() {
                public void run() {
                    //call method to set up device communication
                    UsbInterface intf = usbDevice.getInterface(0);
                    connection.claimInterface(intf, false);

                    //connection settings
                    int op= connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset   //0x40
                    int op2= connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);//clear Rx  
                    int op3= connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
                    int op3b=  connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);//control flow

                    int op4= connection.controlTransfer(0x40, 0x03, 0x001A, 0, null, 0, 0);// baud rate 115200
                    int op5= connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);//8 bit




                    int endPts = intf.getEndpointCount();

                    for(int e = 0; e < endPts; e++){
                        UsbEndpoint endpoint = intf.getEndpoint(e);
                        endpoint.getAttributes();

                        if( endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK){
                            if(endpoint.getDirection() == UsbConstants.USB_DIR_IN){
                                input = endpoint;
                                Log.d("Endpoint", "Got input");


                            }else if(endpoint.getDirection() == UsbConstants.USB_DIR_OUT){
                                output = endpoint;
                                Log.d("Endpoint", "Got output");
                            }                       
                        }
                    }
                   int f =  connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
                   int f2 = connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
                                                    0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
                   System.out.println(f +"  "+ f2 ); 
                   }
                });
            thread.start();




        }
    }

问题是我有一个USB红外线和我的Android设备。我的Android设备有2个USB端口。我想将这个字节“55 AA 81 8 F7 A F5 0”发送到红外线主机。 我知道这有点复杂,但谢谢大家!

2 个答案:

答案 0 :(得分:1)

从我可以看到的问题是,你正在期待字符串&#34; 55 AA 81 8 F7 A F5 0&#34;通过调用String.getBytes()来解释为0x55 0xAA 0x81 0x08 0xF7 0x0A 0xF5 0x00,这不是您将要获得的。除了你想要的字节数组,你将获得代表字符串字符的字节数组(包括空格)。

取代

String s = "55 AA 81 8 F7 A F5 0";
byte [] bytes = s.getBytes();

您需要简单地指定字节数组,如下所示:

byte[] bytes = {
  0x55,
  0xAA, 
  0x81,
  0x08,
  (byte) 0xF7,
  0x0A,
  (byte) 0xF5,
  0x00};

答案 1 :(得分:0)

@ user4610842,您检查了答案吗?怎么样

 byte[] bytes = {
  0x55,
  0xAA, 
  0x81,
  0x08,
  (byte) 0xF7,
  0x0A,
  (byte) 0xF5,
  0x00};

0xAA和0x81在Java中都是int字节。