마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드에서 HidD_SetFeature같은 USB 제어를 진행하려면 어떻게 해야 하나요?

0 추천

안녕하세요?

Windows C++에서 USB HID 장비를 HidD_SetFeature 함수를 이용하여 다음과 같이 제어하였습니다.

unsigned char feature[9] = {0};
feature[0] = 0;
feature[1] = 0xFE;
if(HidD_SetFeature(hDevice, feature, 9))
 return 0;
return -1;

이와 같은 제어를 안드로이드에서 하려면 어떻게 해야 하나요?

UsbManager에서 디바이스 검색 및 오픈까지는 정상으로 되어 product name, manufacturer name 까지는 정상으로 얻었습니다.

public boolean open(UsbManager manager, UsbDevice device)
{
 UsbDeviceConnection connection = manager.openDevice(device);
 if(connection == null)
  return false;
 
 UsbInterface intf = device.getInterface(0);
 // Claims exclusive access to a UsbInterface. 
 // This must be done before sending or receiving data on 
 // any UsbEndpoints belonging to the interface.
 connection.claimInterface(intf, true);
 // getRawDescriptors can be used to access descriptors 
 // not supported directly via the higher level APIs, 
 // like getting the manufacturer and product names.
 // because it returns bytes, you can get a variety of
 // different data types.
 byte[] rawDescs = connection.getRawDescriptors();
 String manufacturer = "", product = "";

 try
 {
     byte[] buffer = new byte[255];
     int idxMan = rawDescs[14];
     int idxPrd = rawDescs[15];

     int rdo = connection.controlTransfer(UsbConstants.USB_DIR_IN
             | UsbConstants.USB_TYPE_STANDARD, STD_USB_REQUEST_GET_DESCRIPTOR,
             (LIBUSB_DT_STRING << 8) | idxMan, 0, buffer, 0xFF, 0);
     manufacturer = new String(buffer, 2, rdo - 2, "UTF-16LE");

     rdo = connection.controlTransfer(UsbConstants.USB_DIR_IN
                     | UsbConstants.USB_TYPE_STANDARD, STD_USB_REQUEST_GET_DESCRIPTOR,
             (LIBUSB_DT_STRING << 8) | idxPrd, 0, buffer, 0xFF, 0);
     product = new String(buffer, 2, rdo - 2, "UTF-16LE");

  byte [] feature = new byte[9];
  for(int i = 0; i < feature.length; i ++)
   feature[i] = 0;
  feature[1] = (byte)0xFE;
//  이것이 제대로 안되네요....
//  connection.controlTransfer(0x20, 0x09,
//             0x0, 0x01, feature, feature.length, 0);
 } catch (UnsupportedEncodingException e)
 {
 e.printStackTrace();
 }

 return true;
}

여기서 feature를 어떻게 전송해야 하는지 좀 가르쳐 주세요.....

감사합니다.

익명사용자 님이 2013년 9월 24일 질문

1개의 답변

0 추천
feature의 제일 첫 바이트를 떼어버리고 request type를 0x21, value를 0x03 (HID SET_FEATURE)로 설정하면 됩니다.
  byte [] feature = new byte[8];
  for(int i = 0; i < feature.length; i ++)
   feature[i] = 0;
  feature[0] = (byte)0xFE;
  connection.controlTransfer(0x21, 0x09, 0x03, 0x00, feature, feature.length, 0);

 

 

이제야 답변 찾았음.. 님이 2014년 12월 9일 답변
...