如何使用UsbManager检测连接的USB设备?

PKa*_*dam 5 usb android

我是Android编程的新手,我的主要目标是使用FT200XD USB至I2C桥接器通过USB与MCU进行通信。

首先,我试图通过UsbManager检测连接的USB设备。据我了解,在创建弹出窗口时,应该询问用户的连接权限,但不会询问权限。在调试过程中,很明显该控件不会进入广播接收器部分。

我参考了一些示例代码片段,并在下面编写了代码。我不知道我在做什么错。

我已经下载了一个名为“ USB host Controller”的应用程序,它可以检测到FT200XD。这意味着我的平板电脑具有USB主机功能。如果您可以指出正确的方向,或者可以共享整个工作代码,那就太好了。

我的代码如下:

Java文件:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_fullscreen);


        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        mPermissionIntent= PendingIntent.getBroadcast(this, 0, new  Intent(ACTION_USB_PERMISSION), 0); 
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(UsbManager.EXTRA_PERMISSION_GRANTED);
        filter.addAction(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);

        }



 // Broadcast receiver

        public class  mUsbReceiver extends BroadcastReceiver { 

            public void onReceive(Context context, Intent intent) {


                Toast.makeText(getApplicationContext(), 
                  "Inside USB Broadcast", Toast.LENGTH_SHORT).show();

            }
        }
Run Code Online (Sandbox Code Playgroud)

清单文件部分:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.usb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />

     <uses-feature android:name="android.hardware.usb.host" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

         <receiver android:name="mUsbReceiver">

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

            </intent-filter>
         </receiver>

        <activity
            android:name="com.example.usb.FullscreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

             <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
                <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>
Run Code Online (Sandbox Code Playgroud)

Device_filter.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
     <resources>
    <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
    <usb-device vendor-id="1027" product-id="24577" />

    <!-- 0x2341 / Arduino -->
    <usb-device vendor-id="9025" />

    <!-- 0x16C0 / 0x0483: Teensyduino  -->
    <usb-device vendor-id="5824" product-id="1155" />

    <!-- 0x617 / 0x000b: EFPL CC2531 -->
    <usb-device vendor-id="1559" product-id="11" />

    <!-- vendor-id="0x0403" product-id="0x6015" // converted to Int vendor ID and product ID of my FT200XD-->
     <usb-device vendor-id="1027" product-id="24597" />

</resources>
Run Code Online (Sandbox Code Playgroud)

Pre*_*ton 5

您可能想要查看registerReceiver的返回代码-从您在代码中发布的内容来看,我认为这将失败,因为您实际上没有实例化mUsbReceiver。看一下这段代码,该代码是从我编写的有效应用程序中提取的,请注意我设置BroadcastReceiver的方式有所不同,这也说明了插入设备后如何请求权限:

PendingIntent mPermissionIntent = null; 
UsbManager mUsbManager = null;

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (ACTION_USB_PERMISSION.equals(action)) {
            // Permission requested
            synchronized (this) {
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    // User has granted permission
                    // ... Setup your UsbDeviceConnection via mUsbManager.openDevice(usbDevice) ...
                } else {
                    // User has denied permission
                }
            }
        }
        if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            // Device removed
            synchronized (this) {
                // ... Check to see if usbDevice is yours and cleanup ...
            }
        }
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            // Device attached
            synchronized (this) {
                // Qualify the new device to suit your needs and request permission
                if ((usbDevice.getVendorId() == MY_VID) && (usbDevice.getProductId() == MY_PID)) {
                    mUsbManager.requestPermission(usbDevice, mPermissionIntent);
                }
            }
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    // ... App Specific Setup Here ...

    mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);

    // Register an intent filter so we can get permission to connect
    // to the device and get device attached/removed messages
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    // ... More App Specific Setup ...
}
Run Code Online (Sandbox Code Playgroud)

另外-看来,对于我的应用程序,我不需要USB操作额外的意图过滤器或元数据XML。