Andrews Bluetooth dynamic rights

Andrews Bluetooth dynamic rights

Android 7.0 and below

In the IDE's Project - Options menu pops up inside the window, find the left tree structure: Application - Uses Permissions project, then the right will let you check out a bunch of permissions. Here to check on Bluetooth and Bluetooth admin two.
Older Android version, check these two on it.
Android 8.0 and above, also need to run the application code dynamically with permission .
Code inside:
First to implement a function:
procedure RequestPermissionsResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
This function is the result of a dynamic application permissions callback function.
BLE at startup, the application permissions through the following code:
PermissionsService.RequestPermissions([FLocationPermission], RequestPermissionsResult, DisplayRationale);
PermissionsService above code is declared in System.Permissions inside.
The code inside RequestPermissionsResult is in front of the callback function. It is called here, and also fill in the parameters.
The code inside FLocationPermission: string; it from:
procedure TForm6.FormCreate(Sender: TObject);

begin

{$IFDEF ANDROID}

  FLocationPermission := JStringToString(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION);

{$ENDIF}

end;

  

The code which used to JStringToString Uses Androidapi.Helpers;

The above code is also a function of a DisplayRationale:

procedure TForm6.DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);

begin

  TDialogService.ShowMessage('We need to be given permission to discover BLE devices',

    procedure(const AResult: TModalResult)

    begin

      APostRationaleProc;

    end)

end;

  In RequestPermissionsResult this function inside, through the authority to determine whether, if adopted, before going to call the method Bluetooth.

procedure TForm6.RequestPermissionsResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);

begin

  // 1 permissions involved: ACCESS_COARSE_LOCATION

  if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then

    StartBLEDiscovery

  else

    TDialogService.ShowMessage('Cannot start BLE scan as the permission has not been granted');

end;

  

Here, permission to use the phone APP issue of the BLE to get it.
The strange thing is, because it is used in "positioning" rights, APP is running, a window will pop up asking the user, APP targeting permission to use, whether to allow. After allowing a user can indeed use the BLE. The same test mobile phone, if I do not apply for permission to do dynamic, direct call related functions BLE, without any prompting, but did not get out BLE related functions.
Do not separate [Bluetooth] authority? Prompt the user to locate a permission for opening the Bluetooth, a bit strange.

Guess you like

Origin www.cnblogs.com/hnxxcxg/p/12114403.html