Skip to content

Commit

Permalink
New android permission headaches
Browse files Browse the repository at this point in the history
  • Loading branch information
vedderb committed Dec 8, 2022
1 parent a2125d1 commit 74f750f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
4 changes: 3 additions & 1 deletion android/AndroidManifest.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='"1.0"'?>
<manifest package='"vedder.vesctool"' xmlns:android='"http://schemas.android.com/apk/res/android"' android:versionName='"$$VT_VERSION"' android:versionCode='"$${VT_ANDROID_VERSION}"' android:installLocation='"auto"'>
<application android:extractNativeLibs='"true"' android:hardwareAccelerated='"true"' android:name='"org.qtproject.qt5.android.bindings.QtApplication"' android:label='"VESC Tool"' android:icon='"@drawable/icon"' android:requestLegacyExternalStorage='"true"'>
<activity android:configChanges='"orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"' android:name='"org.qtproject.qt5.android.bindings.QtActivity"' android:label='"VESC Tool"' android:screenOrientation='"unspecified"' android:launchMode='"singleTop"'>
<activity android:configChanges='"orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"' android:name='"org.qtproject.qt5.android.bindings.QtActivity"' android:label='"VESC Tool"' android:exported='"true"' android:screenOrientation='"unspecified"' android:launchMode='"singleTop"'>
<intent-filter>
<action android:name='"android.intent.action.MAIN"'/>
<category android:name='"android.intent.category.LAUNCHER"'/>
Expand Down Expand Up @@ -89,4 +89,6 @@
<uses-permission android:name='"android.permission.ACCESS_FINE_LOCATION"' />
<uses-permission android:name='"android.permission.ACCESS_BACKGROUND_LOCATION"' />
<uses-permission android:name='"android.permission.FOREGROUND_SERVICE"' />
<uses-permission android:name='"android.permission.BLUETOOTH_SCAN"'/>
<uses-permission android:name='"android.permission.BLUETOOTH_CONNECT"'/>
</manifest>
3 changes: 3 additions & 0 deletions bleuart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ void BleUart::init()
return;
}

Utility::requestBleScanPermission();
Utility::requestBleConnectPermission();

mDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);

connect(mDeviceDiscoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
Expand Down
46 changes: 45 additions & 1 deletion utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ bool Utility::requestFilePermission()
// https://codereview.qt-project.org/#/c/199162/
QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
if(r == QtAndroid::PermissionResult::Denied) {
QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE", 5000);
QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE", 10000);
r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
if(r == QtAndroid::PermissionResult::Denied) {
return false;
Expand All @@ -288,6 +288,50 @@ bool Utility::requestFilePermission()
#endif
}

bool Utility::requestBleScanPermission()
{
#ifdef Q_OS_ANDROID
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.BLUETOOTH_SCAN");
if(r == QtAndroid::PermissionResult::Denied) {
QtAndroid::requestPermissionsSync( QStringList() << "android.permission.BLUETOOTH_SCAN", 10000);
r = QtAndroid::checkPermission("android.permission.BLUETOOTH_SCAN");
if(r == QtAndroid::PermissionResult::Denied) {
return false;
}
}

return true;
#else
return true;
#endif
#else
return true;
#endif
}

bool Utility::requestBleConnectPermission()
{
#ifdef Q_OS_ANDROID
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.BLUETOOTH_CONNECT");
if(r == QtAndroid::PermissionResult::Denied) {
QtAndroid::requestPermissionsSync( QStringList() << "android.permission.BLUETOOTH_CONNECT", 10000);
r = QtAndroid::checkPermission("android.permission.BLUETOOTH_CONNECT");
if(r == QtAndroid::PermissionResult::Denied) {
return false;
}
}

return true;
#else
return true;
#endif
#else
return true;
#endif
}

bool Utility::hasLocationPermission()
{
#ifdef Q_OS_ANDROID
Expand Down
2 changes: 2 additions & 0 deletions utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Utility : public QObject
Q_INVOKABLE static QString uuid2Str(QByteArray uuid, bool space);
Q_INVOKABLE static bool requestFilePermission();
Q_INVOKABLE static bool hasLocationPermission();
Q_INVOKABLE static bool requestBleScanPermission();
Q_INVOKABLE static bool requestBleConnectPermission();
Q_INVOKABLE static void keepScreenOn(bool on);
Q_INVOKABLE static void allowScreenRotation(bool enabled);
Q_INVOKABLE static bool waitSignal(QObject *sender, QString signal, int timeoutMs);
Expand Down
6 changes: 3 additions & 3 deletions vesc_tool.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ VT_CONFIG_VERSION = 2
# Set to 0 for stable versions and to test version number for development versions.
VT_IS_TEST_VERSION = 0

VT_ANDROID_VERSION_ARMV7 = 114
VT_ANDROID_VERSION_ARM64 = 115
VT_ANDROID_VERSION_X86 = 116
VT_ANDROID_VERSION_ARMV7 = 120
VT_ANDROID_VERSION_ARM64 = 121
VT_ANDROID_VERSION_X86 = 122

VT_ANDROID_VERSION = $$VT_ANDROID_VERSION_X86

Expand Down

0 comments on commit 74f750f

Please sign in to comment.