2016-05-23 38 views
2

我尝试发送命令到我的USB自定义设备。我认为我将所有设置妥当 - 例如我可以获取设备的ID,以便Android“看到”它。然而,当我尝试发送它的命令时,我得到了一行空指针:USBConnection nullPointerException

     connection.bulkTransfer(usbEndpointOut,send,send.length,SEND_TIMEOUT); 

端点设置正确(我在日志中检查过它)。这是我的班级。请帮忙。

mTestButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
       HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList(); 
       UsbDevice device; 
       if (usbDevices != null) { 
        boolean keep = true; 
        for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { 
         device = entry.getValue(); 
         int deviceVID = device.getVendorId(); 
         int devicePID = device.getProductId(); 
         if (deviceVID != 0x1d6b || (devicePID != 0x0001 || devicePID != 0x0002 || devicePID != 0x0003)) { 
          Toast.makeText(MainActivity.this, "ID: " + device.getDeviceId() 
            , Toast.LENGTH_SHORT).show(); 
          Log.e(TAG, "onCreate: " + device.getDeviceId()); 
          UsbInterface usbInterface = null; 
          UsbEndpoint usbEndpointIn = null; 
          UsbEndpoint usbEndpointOut = null; 
          for (int i = 0; i < device.getInterfaceCount(); i++) { 
           usbInterface = device.getInterface(i); 
           //l("Interface[" + i + "] -> " + usbInterface); 
           if (usbInterface != null) { 
            for (int j = 0; j < usbInterface.getEndpointCount(); j++) { 
             UsbEndpoint usbEndpoint = usbInterface.getEndpoint(j); 
             //l("Endpoint[" + j + "] -> " + usbEndpoint); 
             if (usbEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
              if (usbEndpoint.getDirection() == UsbConstants.USB_DIR_IN) { 
               //l("Found input!"); 
               usbEndpointIn = usbEndpoint; 
              } else { 
               //l("Found output!"); 
               usbEndpointOut = usbEndpoint; 
              } 
              if (usbEndpointIn != null && usbEndpointOut != null) { 
               break; 
              } 
             } 
            } 
            if (usbEndpointIn != null && usbEndpointOut != null) { 
             break; 
            } 
           } else { 
            //l("Interface was null"); 
           } 
          } 
          connection = usbManager.openDevice(device); 
          connection.bulkTransfer(usbEndpointOut, send, send.length, SEND_TIMEOUT); 
          keep = false; 
         } else { 
          { 
           connection = null; 
           device = null; 
          } 
          if (!keep) 
           break; 
         } 
        } 
       } 
      } 

     }); 
    } 

    private static final byte[] send = new byte[]{ 
      (byte) 0xDA, (byte) 0xAD, // const 
      (byte) 0x02, (byte) 0x74, (byte) 0x00, // com 
      (byte) 0xBF, (byte) 0xDB // last 
    }; 

回答

0

首先,它搜索usbEndpointInusbEndpointOut可能会失败,然后产生null循环。因此,只有拥有有效的终端时,才应该拨打bulkTransfer

但主要的一点是,你错过声称要与之通信的接口,通过bulkTransfer

if (usbEndpointOut != null) { 
    connection = usbManager.openDevice(device); 
    if (connection != null) { 
     if (connection.claimInterface(usbInterface, true) == true) { 
      connection.bulkTransfer(usbEndpointOut, send, send.length, SEND_TIMEOUT); 
      keep = false; 
     } 
    } 
}