【问题标题】:Android USB- "could not find endpoint"Android USB-“找不到端点”
【发布时间】:2012-06-11 21:31:09
【问题描述】:

我是一名初级 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;
        }
    }
}

}

【问题讨论】:

    标签: android usb endpoint


    【解决方案1】:
    // 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。

    【讨论】:

    • 感谢您的回复,是的,这是从他们给出的谷歌 MissileLauncherActivity.java 示例中获取的。我一直在重写它以使用批量传输而不是受控传输。我将“intf.getEndpointCount() != 1”行更改为“> 0”
    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多