2014-07-08 3 views
3

anyOne이이 코드를 도와 줄 수 있습니까?안드로이드에서 호스트 USB 장치로 데이터를 보내는 방법

안드로이드에서 USB로 전송하고 싶습니다. 내 안드로이드 코드에서 외부 USB 장치로 데이터를 보내려고 기술적 인 문제가 있습니다. 나는 판매 인과 다른 정보가있다;

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 적외선 내 안드로이드 장치도를 가지고있다 :

<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 

이 내 코드는 다음과 같습니다

이 내 장치 공급 업체 정보입니다. 내 안드로이드 장치는 2 개의 USB 포트가 있습니다. 적외선 호스트에 "55 AA 81 8 F7 A F5 0"바이트를 보내고 싶습니다. 조금 복잡하지만 모두에게 감사드립니다.

+1

감사합니다. :) – AndrewC

+0

무엇이 잘못되었고 무엇을하고 싶습니까? – AndrewC

답변

1

내가 볼 수있는 문제는 문자열 "55 AA 81 8 F7 A F5 0"이 0x55 0xAA 0x81 0x08 0xF7 0x0A 0xF5 0x00 String.getBytes 호출로 해석 될 것으로 예상한다는 것입니다.(), 당신이 얻으려고하는 것이 아닙니다. 원하는 바이트 배열 대신, 문자열의 문자를 나타내는 바이트 배열을 가져옵니다 (공백 포함).

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}; 
관련 문제