Skip to content

Commit 730a425

Browse files
committed
fix: BLE won't work on Android 11 and below
Changes: - revert c22c243 (#936) - only ask for Location and show Prominent Disclosure on Android 11 and below
1 parent 8cffa70 commit 730a425

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

Diff for: android/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
1313
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
1414

15+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
16+
android:maxSdkVersion="30"/>
17+
1518
<application
1619
android:name=".BadgeMagicApp"
1720
android:allowBackup="true"

Diff for: android/src/main/java/org/fossasia/badgemagic/others/BadgeMagicPermission.kt

+25-1
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ class BadgeMagicPermission private constructor() {
1616

1717
val allPermissions = arrayOf(
1818
Manifest.permission.WRITE_EXTERNAL_STORAGE,
19+
Manifest.permission.ACCESS_FINE_LOCATION,
1920
Manifest.permission.BLUETOOTH_SCAN,
2021
Manifest.permission.BLUETOOTH_CONNECT,
2122
)
2223

2324
val storagePermissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
2425

26+
val locationPermissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
27+
2528
val bluetoothPermissions = arrayOf(
2629
Manifest.permission.BLUETOOTH_CONNECT,
2730
Manifest.permission.BLUETOOTH_SCAN,
2831
)
2932

3033
val ALL_PERMISSION = 100
3134
val STORAGE_PERMISSION = 101
35+
val LOCATION_PERMISSION = 102
3236
val BLUETOOTH_PERMISSION = 103
3337

3438
val listPermissionsNeeded = arrayListOf<String>()
@@ -49,6 +53,13 @@ class BadgeMagicPermission private constructor() {
4953
}
5054
}
5155
}
56+
if (mode == LOCATION_PERMISSION) {
57+
for (permission in locationPermissions) {
58+
if (ContextCompat.checkSelfPermission(activity, permission) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
59+
listPermissionsNeeded.add(permission)
60+
}
61+
}
62+
}
5263
if (mode == BLUETOOTH_PERMISSION) {
5364
for (permission in bluetoothPermissions) {
5465
if (ContextCompat.checkSelfPermission(activity, permission) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
@@ -58,7 +69,20 @@ class BadgeMagicPermission private constructor() {
5869
}
5970
if (listPermissionsNeeded.size > 0) {
6071
for (permission in listPermissionsNeeded) {
61-
if (permission == Manifest.permission.WRITE_EXTERNAL_STORAGE) {
72+
if (permission == Manifest.permission.ACCESS_FINE_LOCATION && android.os.Build.VERSION.SDK_INT <= 30) {
73+
AlertDialog.Builder(activity)
74+
.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_caution))
75+
.setTitle(activity.getString(R.string.location_required_title))
76+
.setMessage(activity.getString(R.string.location_required_message))
77+
.setPositiveButton("OK") { _, _ ->
78+
activity.requestPermissions(locationPermissions, REQUEST_PERMISSION_CODE)
79+
}
80+
.setNegativeButton("Cancel") { _, _ ->
81+
Toast.makeText(activity, activity.getString(R.string.location_canceled_warning), Toast.LENGTH_SHORT).show()
82+
}
83+
.create()
84+
.show()
85+
} else if (permission == Manifest.permission.WRITE_EXTERNAL_STORAGE) {
6286
AlertDialog.Builder(activity)
6387
.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_caution))
6488
.setTitle(activity.getString(R.string.storage_required_title))

Diff for: android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.fossasia.badgemagic.ui
22

3+
import android.Manifest
34
import android.app.Activity
45
import android.app.AlertDialog
56
import android.content.DialogInterface
@@ -224,7 +225,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
224225
private fun prepareForScan() {
225226
if (isBleSupported()) {
226227
val permission = BadgeMagicPermission.instance
227-
permission.checkPermissions(this, permission.BLUETOOTH_PERMISSION)
228+
permission.checkPermissions(this, permission.LOCATION_PERMISSION)
228229
} else {
229230
Toast.makeText(this, "BLE is not supported", Toast.LENGTH_LONG).show()
230231
finish()
@@ -293,6 +294,15 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
293294
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
294295
Timber.d { "Required Permission Accepted" }
295296
}
297+
for (p in permissions) {
298+
if (
299+
p == Manifest.permission.ACCESS_FINE_LOCATION &&
300+
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
301+
) {
302+
val permission = BadgeMagicPermission.instance
303+
permission.checkPermissions(this, permission.BLUETOOTH_PERMISSION)
304+
}
305+
}
296306
}
297307
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
298308
}

Diff for: android/src/main/java/org/fossasia/badgemagic/util/BluetoothAdapter.kt

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package org.fossasia.badgemagic.util
33
import android.app.AlertDialog
44
import android.bluetooth.BluetoothManager
55
import android.content.Context
6+
import android.content.Intent
7+
import android.location.LocationManager
8+
import android.provider.Settings
69
import android.widget.Toast
710
import androidx.core.content.ContextCompat
811
import org.fossasia.badgemagic.R
@@ -12,7 +15,7 @@ class BluetoothAdapter(appContext: Context) {
1215
private val btAdapter = btManager.adapter!!
1316

1417
fun isTurnedOn(context: Context): Boolean = when {
15-
btAdapter.isEnabled -> true
18+
btAdapter.isEnabled -> scanLocationPermissions(context)
1619
else -> {
1720
showAlertDialog(context)
1821
false
@@ -25,6 +28,22 @@ class BluetoothAdapter(appContext: Context) {
2528
}
2629
}
2730

31+
private fun scanLocationPermissions(context: Context): Boolean {
32+
val lm = context.getSystemService(Context.LOCATION_SERVICE)
33+
if (lm is LocationManager) {
34+
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) && android.os.Build.VERSION.SDK_INT <= 30) {
35+
AlertDialog.Builder(context)
36+
.setMessage(R.string.no_gps_enabled)
37+
.setPositiveButton("OK") { _, _ -> context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) }
38+
.setNegativeButton("Cancel", null)
39+
.show()
40+
return false
41+
}
42+
return true
43+
}
44+
return false
45+
}
46+
2847
private fun showAlertDialog(context: Context) {
2948
val dialogMessage = context.getString(R.string.enable_bluetooth)
3049
val builder = AlertDialog.Builder(context)

Diff for: android/src/main/res/values-zh-rCN/strings.xml

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
<string name="clipart_saved_success">ClipArt已成功保存</string>
9494
<string name="clipart_saved_error">保存剪贴画时出错</string>
9595
<string name="drawer_saved_cliparts">保存的教具</string>
96+
<string name="no_gps_enabled">找不到GPS,请从设置中启用GPS</string>
97+
98+
<string name="location_required_title">需要位置许可</string>
99+
<string name="location_required_message">
100+
Badge Magic 会收集位置数据以启用蓝牙低功耗并连接到 LED 徽章并将数据传输到徽章,即使应用程序关闭或未使用也是如此。\n不会将任何位置数据传输到外部设备或我们的服务器。
101+
</string>
102+
<string name="location_canceled_warning">请允许位置权限。 蓝牙 LE 需要这个才能工作。</string>
103+
96104

97105
<string name="storage_required_title">需要存储权限</string>
98106
<string name="storage_required_message">Badge Magic 需要访问存储空间才能保存和访问已保存的徽章/剪贴画</string>

Diff for: android/src/main/res/values/strings.xml

+12
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@
108108
<string name="clipart_saved_success">ClipArt Saved Successfully</string>
109109
<string name="clipart_saved_error">Error in saving Clipart</string>
110110
<string name="drawer_saved_cliparts">Saved Cliparts</string>
111+
<string name="no_gps_enabled">No GPS Found, Please Enable GPS from Settings</string>
112+
113+
<string name="location_required_title">Location Permission Required</string>
114+
<string name="location_required_message">
115+
Badge Magic uses location data to enable Bluetooth LE and to transfer
116+
data to the badges. No location data is transferred to external devices
117+
or our servers.
118+
</string>
119+
<string name="location_canceled_warning">
120+
Please allow Location permissions.
121+
Bluetooth LE requires this to work.
122+
</string>
111123

112124
<string name="storage_required_title">Storage Permission Required</string>
113125
<string name="storage_required_message">Badge Magic requires access to storage to save and access saved Badges/Cliparts.</string>

0 commit comments

Comments
 (0)