altosdroid: Add BLUETOOTH_CONNECT permission, handle its lack
authorKeith Packard <keithp@keithp.com>
Tue, 11 Jan 2022 22:32:55 +0000 (14:32 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 11 Jan 2022 22:46:48 +0000 (14:46 -0800)
Android 12 switched how bluetooth permissions work, so now we need a
new one to talk to devices. Also deal with a lack of that by not
crashing when we can't get the name of a BT device.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/app/build.gradle.in
altosdroid/app/src/main/AndroidManifest.xml.in
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/DeviceListActivity.java

index 763b1657675f91897755ace5971573949cd6d666..dd1a7d97375fa7323c2d4c5686a33863f563db26 100644 (file)
@@ -18,7 +18,7 @@ android {
     defaultConfig {
         applicationId "org.altusmetrum.AltosDroid"
         minSdkVersion 21
-        targetSdkVersion 29
+        targetSdkVersion 30
         versionCode @ANDROID_VERSION@
         versionName "@VERSION@"
     }
index efedddaf5555b4526bab4e615143a7b367579a13..ec84b457fae07a0c8b2f2b2d841e88000508ff64 100644 (file)
@@ -28,6 +28,7 @@
     <!-- Permissions needed to access bluetooth -->
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
+    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
     <!-- Permissions needed to save Telemetry logs to SD card -->
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <!-- Permissions needed for GoogleMaps -->
index 05a023acde3d60f51d27b3caddc1043291336b3c..cf54aad059de146b74e9aecb92d33b051cbf88eb 100644 (file)
@@ -793,8 +793,12 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
 
        public boolean have_location_permission = false;
        public boolean have_storage_permission = false;
+       public boolean have_bluetooth_permission = false;
+       public boolean have_bluetooth_connect_permission = false;
        public boolean asked_permission = false;
 
+       static final String BLUETOOTH_CONNECT = "android.permission.BLUETOOTH_CONNECT";
+
        AltosMapOnline map_online;
 
        void
@@ -817,6 +821,12 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                        if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                                have_storage_permission = true;
                                        }
+                                       if (permissions[i].equals(Manifest.permission.BLUETOOTH)) {
+                                               have_bluetooth_permission = true;
+                                       }
+                                       if (permissions[i].equals(BLUETOOTH_CONNECT)) {
+                                               have_bluetooth_connect_permission = true;
+                                       }
                                }
                        }
                }
@@ -842,15 +852,39 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                        {
                                have_storage_permission = true;
                        }
-                       int count = (have_location_permission ? 0 : 1) + (have_storage_permission ? 0 : 1);
+                       if (ActivityCompat.checkSelfPermission(this,
+                                                              Manifest.permission.BLUETOOTH)
+                           == PackageManager.PERMISSION_GRANTED)
+                       {
+                               have_bluetooth_permission = true;
+                       }
+                       if (ActivityCompat.checkSelfPermission(this,
+                                                              BLUETOOTH_CONNECT)
+                           == PackageManager.PERMISSION_GRANTED)
+                       {
+                               have_bluetooth_connect_permission = true;
+                       }
+                       int count = 0;
+                       if (!have_location_permission)
+                               count += 1;
+                       if (!have_storage_permission)
+                               count += 1;
+                       if (!have_bluetooth_permission)
+                               count += 1;
+                       if (!have_bluetooth_connect_permission)
+                               count += 1;
                        if (count > 0)
                        {
                                String[] permissions = new String[count];
                                int i = 0;
                                if (!have_location_permission)
                                        permissions[i++] = Manifest.permission.ACCESS_FINE_LOCATION;
-                               if (!have_location_permission)
+                               if (!have_storage_permission)
                                        permissions[i++] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
+                               if (!have_bluetooth_permission)
+                                       permissions[i++] = Manifest.permission.BLUETOOTH;
+                               if (!have_bluetooth_connect_permission)
+                                       permissions[i++] = BLUETOOTH_CONNECT;
                                ActivityCompat.requestPermissions(this, permissions, MY_PERMISSION_REQUEST);
                        }
                }
index 60fba9d367126bec3317e120fd05c0bee3a00381..a3aef896c1f4718be190509239959c715e02d056 100644 (file)
@@ -105,10 +105,11 @@ public class DeviceListActivity extends Activity {
                // If there are paired devices, add each one to the ArrayAdapter
                if (pairedDevices.size() > 0) {
                        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
-                       for (BluetoothDevice device : pairedDevices)
-                               if (device.getName().startsWith("TeleBT"))
-                                       mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
-
+                       for (BluetoothDevice device : pairedDevices) {
+                               String name = device.getName();
+                               if (name != null && name.startsWith("TeleBT"))
+                                       mPairedDevicesArrayAdapter.add(name + "\n" + device.getAddress());
+                       }
                } else {
                        String noDevices = getResources().getText(R.string.none_paired).toString();
                        mPairedDevicesArrayAdapter.add(noDevices);
@@ -205,7 +206,7 @@ public class DeviceListActivity extends Activity {
                                {
                                        String  name = device.getName();
                                        if (name != null && name.startsWith("TeleBT"))
-                                               mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
+                                               mNewDevicesArrayAdapter.add(name + "\n" + device.getAddress());
                                }
 
                        /* When discovery is finished, change the Activity title