usb通信

public class USBConnectionManager {
   private UsbManager manager;
   private UsbDevice mUsbDevice;
   private UsbInterface mInterface;
   private UsbEndpoint usbEpOut;

   private UsbEndpoint usbEpIn;
   private UsbDeviceConnection mDeviceConnection;
   private final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
   private OnUSBInitListener mOnUSBInitListener;

   public USBConnectionManager(Context context, int vID, int pID, OnUSBInitListener onUSBInitListener) {
      this.mOnUSBInitListener = onUSBInitListener;
      manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
      HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
      Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
      while (deviceIterator.hasNext()) {
         UsbDevice device = deviceIterator.next();
         if (device.getVendorId() == vID && device.getProductId() == pID) {
            mUsbDevice = device;
            break;
         }
      }
      if (mUsbDevice != null) {
         mInterface = mUsbDevice.getInterface(0);
         if (mInterface != null) {
            if (mInterface.getEndpoint(0) != null) {
               usbEpIn = mInterface.getEndpoint(0);
            }
            if (mInterface.getEndpoint(1) != null) {
               usbEpOut = mInterface.getEndpoint(1);
            }

            if (manager.hasPermission(mUsbDevice)) {
               getConnection();
            } else {
               IntentFilter filter = new IntentFilter();
               filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
               filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
               filter.addAction(ACTION_USB_PERMISSION);
               context.registerReceiver(receiver, filter);
               PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
                     new Intent(ACTION_USB_PERMISSION), 0);

               manager.requestPermission(mUsbDevice, mPermissionIntent);
            }
         } else {
            onUSBInitListener.error(OnUSBInitListener.NOT_FOUND_USBINTERFACE);
         }
      } else {
         onUSBInitListener.error(OnUSBInitListener.NOT_FOUND_DEVICE);
      }
   }

   private void getConnection() {
      mDeviceConnection = manager.openDevice(mUsbDevice);
      if (mDeviceConnection == null) {
         mOnUSBInitListener.error(OnUSBInitListener.OPEN_DEVICE_FAILURE);
      } else {
         mOnUSBInitListener.success();
      }
   }

   public int read(byte[] data) {
      return mDeviceConnection.bulkTransfer(usbEpIn, data, data.length, 3000);
   }

   public void write(byte[] data) {
      mDeviceConnection.bulkTransfer(usbEpOut, data, data.length, 3000);
   }

   public void close() {
      if (mDeviceConnection != null && mInterface != null) {
         mDeviceConnection.releaseInterface(mInterface);
         mDeviceConnection.close();
      }
   }


   private BroadcastReceiver receiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
            boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
            if (granted)
               getConnection();
            else
               mOnUSBInitListener.error(OnUSBInitListener.NO_PERMISSION);
            context.unregisterReceiver(receiver);
         }
      }
   };

   public interface OnUSBInitListener {
      final int NOT_FOUND_DEVICE = -1;
      final int OPEN_DEVICE_FAILURE = -2;
      final int NOT_FOUND_USBINTERFACE = -3;
      final int NO_PERMISSION = -4;

      void success();
      void error(int code);
   }

使用方法

1.初始化

USBConnectionManager manager=new USBConnectionManager(getApplicationContext(), 1234, 5678, new OnUSBInitListener() {

            @Override
            public void success() {

            }

            @Override
            public void error(int code) {

            }
});

初始化参数中需要传context和usb设备的vid和pid,注意这里是10进制的,如果不知道硬件id的,可以吧硬件插入电脑上,在设备管理器里查看。

2.读写数据

写数据直接调用manager.write(byre[] data)方法,参数为要写的数据,返回值为写入数据长度代表写入成功,为负数则失败。

读数据调用manager.read(byte[] data)方法,参数代表要接受的数据,返回值代表读取的数据长度。

3.关闭

manager.close();

相关文章
相关标签/搜索