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.初始化android

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的,能夠吧硬件插入電腦上,在設備管理器裏查看。ide

2.讀寫數據this

寫數據直接調用manager.write(byre[] data)方法,參數爲要寫的數據,返回值爲寫入數據長度表明寫入成功,爲負數則失敗。spa

讀數據調用manager.read(byte[] data)方法,參數表明要接受的數據,返回值表明讀取的數據長度。code

3.關閉get

manager.close();it

相關文章
相關標籤/搜索