2012-06-11 6 views
1

Я начинающий программист для Android, который пытается настроить некоторые функции USB с помощью своего планшета и устройства. Моя программа использует фильтр Intent для поиска устройства. Я взял пример Android MissileLauncherActivity.java и переписал его. Я получаю ошибку «не удалось найти конечную точку» в logcat. Что-то не так с моим кодом, или это может быть мое устройство? LogCat находит ошибку в методе setDevice, где он пытается найти конечную точку OUT. Вот код:Android USB- «не удалось найти конечную точку»

public class UsbTestActivity extends Activity implements Runnable { 

private UsbManager mUsbManager; 
private UsbDevice mDevice; 
private UsbDeviceConnection mConnection; 
private UsbEndpoint mEndpointIntr; 
private static final String TAG = "UsbTestActivity"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE); 
} 

@Override 
public void onPause(){ 
    super.onPause(); 
} 

@Override 
public void onResume(){ 
    super.onResume(); 

    Intent intent = getIntent(); 
    Log.d(TAG, "intent: " + intent); 
    String action = intent.getAction(); 
    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) { 
     setDevice(device); 
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { 
     if (mDevice != null && mDevice.equals(device)) { 
      setDevice(null); 
     } 
    } 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

public void setDevice(UsbDevice device){ 
    Log.d(TAG, "setDevice " + device); 
    if (device.getInterfaceCount() != 1) { 
     Log.e(TAG, "could not find interface"); 
     return; 
    } 
    UsbInterface intf = device.getInterface(0); 
    // device should have one endpoint 
    if (intf.getEndpointCount() != 1) { 
     Log.e(TAG, "could not find endpoint"); 
     return; 
    } 
    System.out.println("Got endpoint"); 
    // endpoint should be of type interrupt 
    UsbEndpoint ep = intf.getEndpoint(0); 
    if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) { 
     Log.e(TAG, "endpoint is not interrupt type"); 
     return; 
    } 
    mDevice = device; 
    mEndpointIntr = ep; 
    if (device != null) { 
     UsbDeviceConnection connection = mUsbManager.openDevice(device); 
     if (connection != null && connection.claimInterface(intf, true)) { 
      Log.d(TAG, "open SUCCESS"); 
      mConnection = connection; 
      Thread thread = new Thread(this); 
      thread.start(); 

     } else { 
      Log.d(TAG, "open FAIL"); 
      mConnection = null; 
     } 
    } 
} 


private void sendCommand(int control) { 
    synchronized (this) { 
     if (mConnection != null) { 
      byte[] message = new byte[1]; 
      message[0] = (byte)control; 
      // Send command via a control request on endpoint zero 
      if(mConnection.controlTransfer(0x21, 0x9, 0x200, 0, message, message.length, 0) > 0){ 
       Log.d(TAG, "Sending Failed"); 
     } 
    } 
} 
} 

public void run() { 
    ByteBuffer buffer = ByteBuffer.allocate(1); 
    UsbRequest request = new UsbRequest(); 
    request.initialize(mConnection, mEndpointIntr); 
    byte status = -1; 
    while (true) { 
     // queue a request on the interrupt endpoint 
     request.queue(buffer, 1); 
     // send poll status command 
     if (mConnection.requestWait() == request) { 
      byte newStatus = buffer.get(0); 
      if (newStatus != status) { 
       Log.d(TAG, "got status " + newStatus); 
       status = newStatus; 
       sendCommand(7); 

      } 
      try { 
       Thread.sleep(100); 
      } catch (InterruptedException e) { 
      } 
     } else { 
      Log.e(TAG, "requestWait failed, exiting"); 
      break; 
     } 
    } 
} 

} 

ответ

0
// device should have one endpoint 
    if (intf.getEndpointCount() != 1) { 
     Log.e(TAG, "could not find endpoint"); 
     return; 
    } 

Похоже, вы пытаетесь работать с USB HID устройства и его OUT конечной точки. Но HID обязывает конечную точку IN присутствовать - поэтому getEndpointCount() скорее всего вернет 2 вместо одного. Вам нужно будет проверить, какой из IN и какой OUT.

+0

Спасибо за ответ. Да, это взято из примера Google MissileLauncherActivity.java, который они дали. Я переписывал его для работы с массовым переносом вместо контролируемого. Я изменил строку «intf.getEndpointCount()! = 1", чтобы быть "> 0" – Dogz1

Смежные вопросы