Hot to get Android USB device permisson with QAndroidJniObject?

  • Hot to get Android USB device permisson with QAndroidJniObject.

    Hello.I have an android device which should work as a usb host. At first i tried to use an intent approach as described here, and although i got a permission request dialog, the permission didn't apply. So i decided to get permissions from user directly.

    Here's the java code i found that is used to request permissions.

    public class MyUsbDevice extends Activity {
        private static MyUsbDevice m_instance;
        private static UsbManager m_usbManager;
        private static PendingIntent mPermissionIntent;
        private static HashMap<String, Integer> deviceCache = new HashMap<String, Integer>();
        private static final String ACTION_USB_PERMISSION = "";
        public MyUsbDevice()
            m_instance = this;
        public void onCreate(Bundle savedInstanceState)
            if (m_usbManager==null) {
                m_usbManager = (UsbManager) m_instance.getSystemService(USB_SERVICE);
            mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            registerReceiver(mUsbReceiver, filter);
        public void onDestroy()
        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 device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            if(device != null){
        public void openDevice(UsbDevice device)
                UsbDeviceConnection devConn = m_usbManager.openDevice(device);
                Integer fd = devConn.getFileDescriptor();
                deviceCache.put(device.getDeviceName(), fd);
        public static int tryOpenDevice()
            HashMap<String, UsbDevice> deviceList = m_usbManager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
            while(deviceIterator.hasNext()) {
                UsbDevice device =;
                    m_usbManager.requestPermission(device, mPermissionIntent);
            return 1;
    //  this call works
        public static int tes()
            return 1;

    And JNI call from C++:

    // this call works
    int res1 = QAndroidJniObject::callStaticMethod<jint>("com/example/myusbdevice/MyUsbDevice", "test", "()I");
    // this doesn't work
    int res2 = QAndroidJniObject::callStaticMethod<jint>("com/example/myusbdevice/MyUsbDevice", "tryOpenDevice", "()I");

    Nothing happens, i don't get any popup dialog. I think is't because i must somehow "create" an activity first, but how?

    I'm very new to android, so any help will be appreciated.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.