Einzelnen Beitrag anzeigen

philipp.hofmann

Registriert seit: 21. Mär 2012
Ort: Hannover
890 Beiträge
 
Delphi 10.4 Sydney
 
#14

AW: Android permissions mit Bluetooth LE und Location werden nicht direkt freigeschal

  Alt 30. Jul 2020, 11:23
Hi Rollo,

jetzt läuft das Demo-Projekt auch bei mir. Ich habe drei Anpassungen vorgenommen:

a) In allen Manifest-Templates die ACCESS_BACKGROUND_LOCATION aufgenommen:

Code:
    <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <%uses-permission%>
b) ACCESS_FINE_LOCATION aufgenommen (war unter Delphi 10.3.3 noch nicht notwendig, ab Delphi 10.4 aber scheinbar schon)

Delphi-Quellcode:
procedure TForm1.Request_LC;
var
    LPermission_LC: String;
    LPermission_LF: String;
    LPermission_LB: String;

begin

    // Location Permissions
    LPermission_LC := JStringToString( TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION );
    LPermission_LF := JStringToString( TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION );
    LPermission_LB := CPermission_AccessBackgroundLocation;

    if CheckBox1.IsChecked then
    begin
        Memo1.Lines.Insert( 0, 'Request LOCATION permission WITH BG ...' );

        PermissionsService.RequestPermissions(
              [ LPermission_LC
                , LPermission_LF
                , LPermission_LB ]
            , EvOnRequestPermissionsResult_LC
            , nil // DisplayRationale
            );
    end
    else
    begin
        Memo1.Lines.Insert( 0, 'Request LOCATION permission FG only ...' );

        PermissionsService.RequestPermissions(
              [ LPermission_LC, LPermission_LF ]
            , EvOnRequestPermissionsResult_LC
            , nil // DisplayRationale
            );
    end;


end;
c) Die Permission-Result-Methode dementsprechend angepasst:

Delphi-Quellcode:
procedure TForm1.EvOnRequestPermissionsResult_LC( Sender : TObject;
                                                  const APermissions : TArray<string>;
                                                  const AGrantResults: TArray<TPermissionStatus>);
var i:integer;
begin


    if CheckBox1.IsChecked then
    begin

        if Length(AGrantResults) >= 3 then
        begin
            if (AGrantResults[0] = TPermissionStatus.Granted ) and
               (AGrantResults[1] = TPermissionStatus.Granted ) and
               (AGrantResults[2] = TPermissionStatus.Granted ) then
            begin
                 Memo1.Lines.Insert( 0, 'Permission LOCATION w/ BG granted' );

                 FRequestSequence := 1;
            end
            else
            begin
                for i:=Length(AGrantResults)-1 downto 0 do
                begin
                  if (AGrantResults[i] = TPermissionStatus.Granted) then
                    Memo1.Lines.Insert( 0, IntToStr(i)+ ': Permission granted' )
                  else
                    Memo1.Lines.Insert( 0, IntToStr(i)+ ': Permission not granted' );
                end;
                Memo1.Lines.Insert( 0, 'Permission LOCATION w/ BG not fully granted' );
                FRequestSequence := 0;
            end;
        end
        else
        begin
             Memo1.Lines.Insert( 0, 'Permission LOCATION w/ BG error' );

             FRequestSequence := 0;
        end;


    end
    else
    begin

        if Length(AGrantResults) >= 2 then
        begin
            if (AGrantResults[0] = TPermissionStatus.Granted ) and
               (AGrantResults[1] = TPermissionStatus.Granted ) then
            begin
                 Memo1.Lines.Insert( 0, 'Permission LOCATION FG granted' );

                 FRequestSequence := 1;
            end
            else
            begin
                for i:=Length(AGrantResults)-1 downto 0 do
                begin
                  if (AGrantResults[i] = TPermissionStatus.Granted) then
                    Memo1.Lines.Insert( 0, IntToStr(i)+ ': Permission granted' )
                  else
                    Memo1.Lines.Insert( 0, IntToStr(i)+ ': Permission not granted' );
                end;
                Memo1.Lines.Insert( 0, 'Permission LOCATION FG not fully granted' );
                FRequestSequence := 0;
            end;
        end
        else
        begin
             Memo1.Lines.Insert( 0, 'Permission LOCATION FG error' );

             FRequestSequence := 0;
        end;


    end;

end;
Je nach Android-Version bekommt man in der Permission-Result-Methode jetzt auch ein "Not fully granted" zurück und muss dann z.B. intern wissen, dass für Android 5.x nur die erste von hier dreien Permission erlaubt sein muss.

Damit ging der Scan (reicht dir das als Result?) auf einem Android-9 und einem Android-10-Device.

Grüße, Philipp
  Mit Zitat antworten Zitat