Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to detect USB device in Android
    primarykey
    data
    text
    <p>I have USB host android device for that I need to connect USB device. to detect usb device to host I written following code.</p> <pre><code>public class ReadData extends Activity { UsbManager usbManager; PendingIntent mPermissionIntent; UsbDevice usbDevice; Intent intent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_data); usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); final String ACTION_USB_PERMISSION = "com.example.udevice.USB_PERMISSION"; IntentFilter filter = new IntentFilter("android.hardware.usb.action.USB_ACCESSORY_ATTACHED"); registerReceiver(mUsbReceiver, filter); } private static final String ACTION_USB_PERMISSION = "com.example.udevice.USB_PERMISSION"; private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); usbManager.requestPermission(usbDevice, mPermissionIntent); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if(usbDevice != null){ //call method to set up device communication int deviceId = usbDevice.getDeviceId(); int productId = usbDevice.getProductId(); Log.i("device id", "****"+deviceId); Log.i("product id", "****"+productId); }else{ Log.i("device id", "No USB device"); } } else { Log.d("shiv", "permission denied for device "); } } } } }; </code></pre> <p>and manifest is like below:</p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.udevice" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-feature android:name="android.hardware.usb.host" /&gt; &lt;uses-sdk android:minSdkVersion="12" android:targetSdkVersion="15" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name=".ReadData" android:label="@string/title_activity_heat_con" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN"/&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>device_filter.xml</p> <pre><code> &lt;resources&gt; &lt;usb-device vendor-id="67b" product-id="2303"/&gt; &lt;/resources&gt; </code></pre> <p>in above xml file I added device attributes. I am expecting a broadcast intent whenever USB device connected to host device. but it is not happening. What is wrong with above code.</p> <p>Thanks shiv</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload