在应用程序启动时已经连接的设备上打开USB主机通信



我正在开发一个应用程序,我需要使用我的平板电脑的usb主机模式管理设备。

此时,我只能在连接设备时激活USB。流程步骤:


    应用程序未启动,设备正在连接。
  1. Android问我是否不想启动我的应用

  2. 我接受然后应用程序启动,我可以使用USB连接。

但是,这不是我想做的:

  1. 平板关机,连接设备



  2. 初始化设备的usb连接。

事实是,实际上,我需要断开/手动连接我的平板电脑上的usb连接,但是我,在我的情况下,设备将已经连接到平板电脑,然后我需要初始化连接,而不断开/重新连接usb。

我已经尝试了谷歌在USB主机连接页面上提供的关于广播接收器的示例,但这不起作用,或者我不太了解它。

我的问题是:
是否有任何方法打开连接到usb主机设备,已经连接?
顺便说一下,我需要搜索找到这个该死的解决方案:D

这里的代码已经实现,以帮助我理解我的问题:

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="..."
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="13" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/..." 
        android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
        <activity
            android:name="...Activity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            >
            <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_ATTACHED" />
            </intent-filter>
            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
            </activity>
    </application>
</manifest>

文件res/xml/device_filter.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device vendor-id="5455" product-id="8238" />
</resources>
然后onResume()在我的Activity:
@Override
protected void onResume() {
    super.onResume();
    Intent intent = getIntent();
    String action = intent.getAction();
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
        UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        UsbManager usbManager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
    }
}

最后我的方法允许连接:

protected void open(Object o) throws DeviceException {
    if (device != null && device.getVendorId() == 5455 && device.getProductId() == 8238) {
        int nbEndPoint = 0;
        for (int i = 0; i < device.getInterfaceCount(); i++) {
            UsbInterface usbInterface = device.getInterface(i);
            for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
                UsbEndpoint usbEndPoint = usbInterface.getEndpoint(j);
                nbEndPoint++;
                switch (nbEndPoint) {
                case 1:
                    this.pipeWriteData = usbEndPoint;
                case 2:
                    this.pipeReadCommandResult = usbEndPoint;
                case 3:
                    this.pipeReadAutoStatus = usbEndPoint;
                case 4:
                    this.pipeReadImageData = usbEndPoint;
                }
            }
        }
        usbConnection = manager.openDevice(device);
        if (usbConnection == null || !usbConnection.claimInterface(device.getInterface(0), true)) {
            usbConnection = null;
            throw new DeviceException(DeviceException.UNKNOW_ERROR);
        }
    }
}

我的猜测是你的应用程序可能建立通信与侦听器寻找连接只是由USB设备。如果打印机已经连接,你的应用程序正在等待那个监听器,它将不会发生,因为你的回调函数将不会被调用,usb打印机已经在你的应用程序运行之前存在。这是一种猜测,因为我没有看到你的代码,但我在其他情况下看到了这种行为。

在问题中获得更多信息后编辑。正如我在评论中所说,我相信如果设备在启动时已经连接,则启动应用程序(…USB_DEVICE_ATTACHED)的事件不会发生。为了证明这一点,你应该在你的代码中添加大量的logcat(查看)和(写入)跟踪,在你的onCreate, onResume和其他地方。这样做,您将看到每个部分是否按您的期望执行。我的猜测是,当你启动和USB设备已经连接时,它根本没有执行。如果我没理解错,你想要的是这个。当设备正在连接时,你想要启动你的应用程序。但如果设备在启动时已经存在,你希望你的应用程序无论如何都要启动并连接到USB。我的方法是让你的应用程序寻找目标USB设备的存在。它的存在与"USB_DEVICE_ATTACHED"不同,"USB_DEVICE_ATTACHED"意味着它在最近几毫秒内刚刚连接。我还将在"USB_DEVICE_ATTACHED"上注册一个侦听器,该侦听器将在设备连接上启动您的代码,我还将在"BOOT_COMPLETED"上设置一个侦听器,以便在平板电脑启动顺序后启动您的应用程序。

最新更新