2016-05-24 114 views
0

我嘗試連接自定義USB設備。我已經做了一切與Android的文檔一步一步,但我仍然在連接對象獲得空指針。這裏是我的活動:USB設備連接時出現NullPointerException

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); 
     IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
     registerReceiver(usbReceiver, filter); 

    usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); 
    HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); 
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
    while (deviceIterator.hasNext()) { 
     UsbDevice device = deviceIterator.next(); 

     if (device.getVendorId() == 1659 && device.getProductId() == 8963) { 
      this.device = device; 
      usbManager.requestPermission(device, mPermissionIntent); 
      Toast.makeText(MainActivity.this, "vendor: " + device.getVendorId() + "ID " + device.getProductId() , Toast.LENGTH_SHORT).show(); 
      break; 
     } 
    } 

這是我嘗試使用USB設備連接:

mTestButton = (Button) findViewById(R.id.button); 
     mTestButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       usbInterface = device.getInterface(0); 
       if (connection != null) { 
        connection = usbManager.openDevice(device); 
        connection.claimInterface(usbInterface, false); 

       for (int i = 0; i < usbInterface.getEndpointCount(); i++) { 
        UsbEndpoint end = usbInterface.getEndpoint(i); 
        if (end.getDirection() == UsbConstants.USB_DIR_IN) { 
         usbEndpointIn = end; 

        } else { 
         usbEndpointOut = end; 
        } 
       } 

       //SEND START COMMAND TO THE USB DEVICE; 
       int result = connection.bulkTransfer(usbEndpointOut, "START".getBytes(), "START".getBytes().length, 1000); 
       Log.e("SEND RESULT", result + ""); 


       //START READING in run method 
       readThread = new Thread((Runnable) MainActivity.this); 
       readThread.start(); 

      } else { 
       Toast.makeText(MainActivity.this, "Connection is null", Toast.LENGTH_SHORT).show(); 
      } 

但連接仍然是空:/幫助,請。

+0

你的USB許可添加到您的AndroidManifest.xml? –

+0

獲取usbReceiver中的連接對象。在接收器中:boolean granted = arg1.getExtras()。getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if(granted){ connection = usbManager.openDevice(device); } – Amit

回答

0

試試這個,

<manifest ...> 
<uses-feature android:name="android.hardware.usb.host" /> 
<uses-sdk android:minSdkVersion="12" /> 
... 
<application> 
    <activity ...> 
     ... 
     <intent-filter> 
      <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
     </intent-filter> 

     <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
      android:resource="@xml/device_filter" /> 
    </activity> 
</application> 
</manifest> 

device_filter.xml

<?xml version="1.0" encoding="utf-8"?> 

<resources> 
    <usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" /> 
</resources> 

更多信息:點擊here

+0

THX Sathish所在,但我有這個聲明,在我的清單和問題依然存在。 – Bartos

+0

試試這個http://stackoverflow.com/questions/28137280/how-to-grant-permission-to-open-usb-device-with-usb-manager-opendevice-always –

+0

我已經完成了一切:/ – Bartos