How to detect USB device in Android

2019-03-16 17:07发布

问题:

I have USB host android device for that I need to connect USB device. to detect usb device to host I written following 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 ");
                        }
                    }
                }
            }
        };

and manifest is like below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.udevice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature android:name="android.hardware.usb.host" />
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ReadData"
            android:label="@string/title_activity_heat_con" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <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>

</manifest>

device_filter.xml

 <resources>
        <usb-device vendor-id="67b" 
            product-id="2303"/>
    </resources>

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.

Thanks shiv

回答1:

I think you need to add:

<intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

It is described here



回答2:

There is onething you are doing it wrong.

The vendor id and device id should be in decimals not in hex. For example, you need to define as follows

 <resources>
        <usb-device vendor-id="1659" 
            product-id="8963"/>
    </resources>

I converted your device id and vendor-id from hex to decimal Let me know if this helps