2

如果我无法清楚地阐述我的问题,请原谅我。我正在尝试与 USB 设备通信,并且正在成功完成,但我想从 USB 设备读取。传输命令的代码如下。

    public final static String TAG = "UsbClient";
private UsbManager mUsbManager;
private final int VID = 0x1fc9;
private final int PID = 0x0003;
    private int toggle = 0;



public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_dummy,
            container, false);
mUsbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    getActivity().registerReceiver(mUsbReceiver, filter);
HashMap<String, UsbDevice> devlist = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviter = devlist.values().iterator();
while (deviter.hasNext()) {
        UsbDevice d = deviter.next();
        print("Found device: "
                + String.format("%04X:%04X", d.getVendorId(),
                        d.getProductId()));
        if (d.getVendorId() == VID && d.getProductId() == PID) {

            mUsbManager.requestPermission(d, mPermissionIntent);
        }

}   

注册接收者的代码是

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                          //call method to set up device communication
                            if (device.getVendorId() == VID && device.getProductId() == PID) {
                                print("Got permission for the device");
                                permissionGranted = true;
                                UsbInterface intf = device.getInterface(0);
                                conn = mUsbManager.openDevice(device);
                                //intf=device.getInterface(0);

                                conn.claimInterface(intf, true);
/*                              if (!conn.claimInterface(device.getInterface(1), true)) {
                                    print("Could not claim the interface");
                                    return;
                                }
*/                              
                                int i = conn.controlTransfer(0x21, 0x09, 0x0200, 0x0000, new byte[] { (byte) toggle }, 1, 0);
                                endpoint=device.getInterface(0).getEndpoint(0);
                                if (toggle == 0) {
                                    toggle = 1; 
                                }
                                else {
                                    toggle = 0;
                                }

                                if ((i == 0) || (i==1)) {
                                    print("transfer successful");
                                }
                                else {
                                    print("transfer failed");
                                }



                            }
                       }
                    } 
                    else {
                        print("permission denied for device " + device);
                    }
                }
            }
        }
    };   

现在我想知道使用 OTG 电缆从连接到 android 平板电脑的 USB 设备读取数据的过程。还有一件事是如何获取端点以读取和写入 USB 设备。

提前致谢。

4

0 回答 0