2016-05-23 14 views
2

Ich versuche, Befehl an mein benutzerdefiniertes USB-Gerät zu senden. Ich denke, ich habe alles richtig eingestellt - zum Beispiel kann ich ID des Geräts bekommen, damit Android es "sieht". Allerdings, wenn ich versuche, es Befehl zu senden I Null-Zeiger in Zeile erhalten:USBConnection nullPointerException

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

Endpunkte richtig eingestellt sind (ich habe es auf Protokoll überprüft). Das ist meine Klasse. Bitte, helfen Sie.

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 
    }; 

Antwort

0

Zunächst einmal die Schleife, die usbEndpointIn und usbEndpointOut sucht scheitern könnte und dann ergibt null. Sie sollten also nur bulkTransfer anrufen, wenn Sie gültige Endpunkte haben.

Aber die Hauptsache ist, dass Sie die Schnittstelle Anspruch verpasst, mit dem Sie kommunizieren möchten über 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; 
     } 
    } 
}