Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Hot to get Android USB device permisson with QAndroidJniObject?

Hot to get Android USB device permisson with QAndroidJniObject?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qandroidjniobjeandroidpermission
1 Posts 1 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dream_captainD Offline
    dream_captainD Offline
    dream_captain
    wrote on last edited by dream_captain
    #1

    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 = "com.android.example.USB_PERMISSION";
    
    
        public MyUsbDevice()
        {
            m_instance = this;
        }
    
    
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(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);
        }
    
        @Override
        public void onDestroy()
        {
            super.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){
                                m_instance.openDevice(device);
                            }
                        }
                    }
                }
            }
        };
    
    
        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 = deviceIterator.next();
                    m_usbManager.requestPermission(device, mPermissionIntent);
                    break;
            }
            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.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved