Bart Kindt
Bart Kindt

Reputation: 326

Delphi 11.2 Bluetooth permision not granted

Running on an Android 12 device. Worked fine in Delphi 11.1.

I have two apps in use. In only one of the two, I can not seem to get Bluetooth permision granted. I have set the Fine Location, which is accepted on initial install. I only require basic Bluetooth, and Bluetooth Admin. When going through the requests one at the time (instead of all in one go) this fails permanently:

PermissionsService.RequestPermissions([cPermissionBluetooth], TakeBluetoothPermissionsResultHandler, DisplayRationale);

And, the DisplayRationale never works at all. During startup, there is no other User request from the system.

In Setup>Apps there is no specific reference to Bluetooth. It does not tell me why it fails. According to https://developer.android.com/guide/topics/connectivity/bluetooth/permissions I have got all related settings okay (including the Location). Targeting 'android:maxSdkVersion="30"' which is set by the compiler as default.

In my other App, which is much more complicated, with the same Bluetooth/Location settings, it works normally.

What could I have missed?

UPDATE: I now changed the code to this:

const
cPermissionBluetooth = 'android.permission.BLUETOOTH';
  cPermissionBluetoothAdmin = 'android.permission.BLUETOOTH_ADMIN';
  cPermissionBluetoothConnect = 'android.permission.BLUETOOTH_CONNECT';
  cPermissionBluetoothScan = 'android.permission.BLUETOOTH_SCAN';

procedure TMainForm.TakeBluetoothPermissionsResultHandler(Sender: TObject; const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray);
begin
  // if TGrantResults(AGrantResults).AreAllGranted then
  if PermissionsService.IsPermissionGranted(cPermissionBluetooth) then
  begin
    DoLog('cPermissionBluetooth: Granted');
  end else
  begin
    DoLog('cPermissionBluetooth: NOT Granted!',d_error);
  end;

  if PermissionsService.IsPermissionGranted(cPermissionBluetoothConnect) then
  begin
    DoLog('cPermissionBluetoothConnect: Granted');
  end else
  begin
    DoLog('cPermissionBluetoothConnect: NOT Granted!',d_error);
  end;

  if PermissionsService.IsPermissionGranted(cPermissionBluetoothScan) then
  begin
    DoLog('cPermissionBluetoothScan: Granted');
  end else
  begin
    DoLog('cPermissionBluetoothScan: NOT Granted!',d_error);
  end;
end;

And what happens is that the basic android.permission.BLUETOOTH is NOT granted, but the other two are. And Bluetooth actually works. Does this mean that I am NOT allowed to call android.permission.BLUETOOTH if running on Android 12 or higher? And that I now must specifically look at the Android version, and NOT call that when 12 or higher??

Why does it work normally in my primary App then..?

Right now, I cannot call 'Are all Permissionas Granted' because one fails all the time. PS: Why can I not use the build-in "TPermissionsService.IsEveryPermissionGranted"? It is imcompatible with its own Android code!

UPDATE2: The final is like this:

   <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

The one which permanently is NOT granted (in Android 12) is: "android.permission.BLUETOOTH".

(I made a mistake in one of my comments about this).

If I ignore this, the Bluetooth does work normally.

Upvotes: 1

Views: 1725

Answers (1)

Bart Kindt
Bart Kindt

Reputation: 326

I had to change my code like this:

if TOSVersion.Major < 12 then
          begin
            LogClient('TOSVersion.Major < 12');
            PermissionsService.RequestPermissions([cPermissionReadExternalStorage,
              cPermissionWriteExternalStorage, cPermissionLocation, cPermissionBluetooth, cPermissionCamera],
              TakeGeneralPermissionsResultHandler, DisplayRationale);
          end else
          begin
            LogClient('TOSVersion.Major >= 12');
            PermissionsService.RequestPermissions([cPermissionReadExternalStorage,
              cPermissionWriteExternalStorage, cPermissionLocation, cPermissionBluetoothConnect, cPermissionBluetoothScan, cPermissionCamera],
              TakeGeneralPermissionsResultHandler, DisplayRationale);
          end;

Upvotes: 3

Related Questions