Android: Get UsbDevice from Intention

I fiddled with the USB host and following the recommendations on the Android developers site I managed to create Hello World, which starts after connecting a specific USB device. However, when I try and "... get a UsbDevice that represents the connected device from intent," it returns null:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); // device is always null if (device == null){Log.i(TAG,"Null device");} 

Here is my manifest:

 <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> </activity> </application> 

And my xml / device_filter.xml (I know that this is the correct VID and PID, because I have a similar application that works using the enumeration method described on the Android developers site :

 <resources> <usb-device vendor-id="1234" product-id="1234"/> </resources> 
+4
source share
2 answers

When your application is (re) launched due to a USB device connection event, then the device is passed into intent when onResume is onResume . You can navigate to it using the getParcelableExtra method. For instance:

 @Override protected void onResume() { super.onResume(); Intent intent = getIntent(); if (intent != null) { Log.d("onResume", "intent: " + intent.toString()); if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (usbDevice != null) { Log.d("onResume", "USB device attached: name: " + usbDevice.getDeviceName()); 
+5
source

I found a workaround (or intended use?) Thanks to Taylor Alexander . Basically, I understand that when you start an intent that opens an application, only the application opens. After that, you need to search and access USB devices through the Enumerating Devices section of the Android developers page in the onResume method.

 @Override public void onResume() { super.onResume(); UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); while(deviceIterator.hasNext()){ UsbDevice device = deviceIterator.next(); // Your code here! } 

I'm not sure if this is the right way to do this, but it seems to work. If anyone has any further suggestions, I would be happy to listen.

0
source

Source: https://habr.com/ru/post/1493140/


All Articles