chris.o.
chris.o.

Reputation: 93

Android USB Permissions Dialog never appears

I've written a simple app to send commands to a USB printer connected to an Android 4.0 tablet via USB. For some reason, I am unable to get past obtaining permissions to claim an interface and open a connection. Here's the relevant code :

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;
          }
        }


}

Here's the relevant part of the manifest:

<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"/>

I've configured the device so that it launches my app whenever the printer is connected, and the device is found during the enumeration (onResume()) and the request for permission is called. However, for whatever reason, I never see the "Request Permission" dialog (I've seen screenshots of it online) nor does onReceive() ever get called. Any ideas why this might be happening?

Upvotes: 3

Views: 7763

Answers (2)

chris.o.
chris.o.

Reputation: 93

So it turns out that SystemUI.apk/SystemUI.odex in /system/ had been change to SystemUI.apk.backup/SystemUI.odex.backup respectively. This presumably was to prevent some aspects of the System UI from mucking up the intended "kiosk" mode of the device. The logcat entry in my previous comment was the big clue. Once the filenames were restored, I started seeing the Permission dialog.

Upvotes: 1

Alin
Alin

Reputation: 1306

1) Why do you have two members ACTION_USB_PERMISSION declared?

2) In onCreate() try to register your Intent like this, maybe there is a difference:

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) Due to a bug in Android (http://code.google.com/p/android/issues/detail?id=25703), I'm not sure if ACTION_USB_ACCESSORY_ATTACHED action is ever called. Try to add 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>

Upvotes: 1

Related Questions