1

我是一名初学者 android 程序员,正在尝试使用我的平板电脑和设备设置一些 USB 功能。我的程序使用 Intent 过滤器来查找设备。我拿了 Android 的 MissileLauncherActivity.java 示例并重写了它。我在 logcat 中收到“找不到端点”错误。我的代码有问题吗,或者可能是我的设备?LogCat 在尝试查找 OUT 端点的 setDevice 方法中查找错误。这是代码:

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;
        }
    }
}

}
4

1 回答 1

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。

于 2012-06-12T13:08:44.967 回答