Android USB权限对话框永远不会出现



我写了一个简单的应用程序,通过USB向连接到Android 4.0平板电脑的USB打印机发送命令。由于某种原因,我无法获得声明接口和打开连接的权限。以下是相关代码:

public class TestPrintActivity extends Activity {
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbInterface mInterface;
private UsbEndpoint mBulkIn;
private UsbEndpoint mBulkOut;
private static final String ACTION_USB_PERMISSION =
        "com.demo.xprinter.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private BroadcastReceiver mUsbReceiver; 
@Override
private static final String ACTION_USB_PERMISSION =
        "com.demo.printerDemo.USB_PERMISSION";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_print);
    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_ACCESSORY_DETACHED);
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
    registerReceiver(mUsbReceiver, filter);
    mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if(device != null){
                          //call method to set up device communication
                            openPort(device);
                       }
                    } 
                    else {
                        Toast.makeText(getApplicationContext(), "Denied!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };
}
public void onResume() {
    super.onResume();
    HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList();
    for (HashMap.Entry<String,UsbDevice> entry : deviceList.entrySet()) {
          UsbDevice aDevice = entry.getValue();
          if ((aDevice.getProductId() == 8965) && (aDevice.getVendorId() == 1659) ){
              if (mUsbManager.hasPermission(aDevice))
                openPort(aDevice);
              else
                mUsbManager.requestPermission(aDevice, mPermissionIntent);
              break;
          }
        }

}

这是清单的相关部分:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.printerDemo"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />
<uses-feature android:name="android.hardware.usb.host"/>

我已经配置了设备,以便它在打印机连接时启动我的应用程序,并且在枚举(onResume())期间找到设备并调用权限请求。然而,无论出于何种原因,我从来没有看到"请求许可"对话框(我在网上看到过它的截图),也没有onReceive()被调用。知道为什么会这样吗?

1)为什么声明了两个成员ACTION_USB_PERMISSION ?

2)在onCreate()尝试注册你的意图像这样,也许有一个区别:

IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    filter.addAction(ACTION_USB_PERMISSION);
context.registerReceiver(usbReceiver, filter);

3)由于一个bug在Android (http://code.google.com/p/android/issues/detail?id=25703),我不确定是否ACTION_USB_ACCESSORY_ATTACHED动作被调用。尝试添加the below in your AndroidManifest.xml:

    <activity ...>
 <intent-filter >
                <action android:name="android.intent.action.MAIN" />                
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
                <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

所以原来SystemUI.apk/SystemUI。/system/中的SystemUI.apk.backup/SystemUI.odex已更改为SystemUI.apk.backup/SystemUI.odex。分别备份。这大概是为了防止系统UI的某些方面弄乱设备的"kiosk"模式。我之前评论中的logcat条目是一个大线索。文件名恢复后,我开始看到Permission对话框。

最新更新