简体   繁体   English

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

[英]How to send data from android to Host USB device

Can anyOne help me with this code?任何人都可以帮助我使用此代码吗?

I would like to transmit from android to USB.我想从android传输到USB。 I Have some technical problems trying to send data from my android code to an external USB device.我在尝试将数据从我的 android 代码发送到外部 USB 设备时遇到了一些技术问题。 I have the vendor and other info;我有供应商和其他信息;

This is my device vendor info:这是我的设备供应商信息:

<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

This is my code:这是我的代码:

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();




        }
    }

The thing is that i have an USB Infrared and my android device as well.问题是我有一个 USB 红外线和我的 android 设备。 My android device has 2 USB ports.我的安卓设备有 2 个 USB 端口。 I would like to send this bytes "55 AA 81 8 F7 A F5 0" to the Infrared Host.我想将此字节“55 AA 81 8 F7 A F5 0”发送到红外主机。 I know that is a little bit complicated but thank you for all!我知道这有点复杂,但谢谢大家!

The problem, from what I can see, is that you're expecting the string "55 AA 81 8 F7 A F5 0" to be interpreted as 0x55 0xAA 0x81 0x08 0xF7 0x0A 0xF5 0x00 with the call to String.getBytes(), which is not what you're going to get.从我所看到的问题是,您希望将字符串“55 AA 81 8 F7 A F5 0”解释为 0x55 0xAA 0x81 0x08 0xF7 0x0A 0xF5 0x00 并调用 String.getBytes(),其中不是你要得到的。 In stead of the byte array you want, you're going to get the byte array which represent the characters of your string (including the spaces).您将获得表示字符串字符(包括空格)的字节数组,而不是您想要的字节数组。

In stead of代替

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

You need to simply specify the byte array, like so:您只需指定字节数组,如下所示:

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

@user4610842, have you checked your answer? @ user4610842,你检查过你的答案吗? how is怎么

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

0xAA and 0x81 are nowhere near a byte they are int in java. 0xAA 和 0x81 远不及它们在 java 中是 int 的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM