in android programming you need to get usb permission before working with connected device to client device.
i am trying to connect a hack-rf to android device and use it... but first of all i need to get usb permission and detect that hackrf.
setup:
1- qt6.3.0
2- ndk 21.3.6528147
3- ui with QML
this is my c++ code to get permission of storage:
bool checkAppPermissions(QString permission)
{
auto result = QtAndroidPrivate::checkPermission(permission).result();
if (result == QtAndroidPrivate::Denied) {
auto result2 = QtAndroidPrivate::requestPermission(permission).result();
qDebug() << permission << result2;
if (result2 == QtAndroidPrivate::Denied)
return false;
else {
return true;
}
}
else {
return true;
}
}
and it's usage:
checkAppPermissions("android.permission.WRITE_EXTERNAL_STORAGE")
but i cannot get usb permisson this way.. its not working.
in AndroidManifest.xml i put these:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.usb.host"/>
also i think the permission that i had to get is "com.android.example.USB_PERMISSION" but i dont know how should i Grant or request for that.
this kotlin code works fine... it detects device and gets the permission but i don't know how it is working! or how can i implement it in Qt with cpp.
i also see this answer but i dont know how he uses C++ native side code or MyActivity.java
could you help me to detect device and grant it's permission at run time.
To ask for USB permission you need to use the Java class called
UsbManager.In Qt6, they changed how QJniObject works, so it is kinda hard to find how to use it but here is what you need to do :
Get the context and usb manager object :
Iterate on the
deviceListobtained fromusbManager:Get your device object and obtain all information needed (if you want to print for debug or something else)
And this is how you request permission to a specific device