krokyze / FitKit

Flutter plugin for reading health and fitness data. Wraps HealthKit on iOS and GoogleFit on Android.
BSD 2-Clause "Simplified" License
98 stars 73 forks source link

Android 10 - ACTIVITY_RECOGNITION #60

Closed Feduch closed 4 years ago

Feduch commented 4 years ago

Hi!

On Android 10 getting PlatformException

I/flutter (11492): PlatformException(FitKit, 10: SecurityException: com.google.step_count.delta requires android.permission.ACTIVITY_RECOGNITION, null) I/flutter (11492): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569) I/flutter (11492): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156) I/flutter (11492): <asynchronous suspension> I/flutter (11492): #2 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:342) I/flutter (11492): <asynchronous suspension> I/flutter (11492): #3 FitKit.read.<anonymous closure> (package:fit_kit/src/fit_kit.dart:0) I/flutter (11492): <asynchronous suspension>

How to fix it?

LaxmikanthMadhyastha commented 4 years ago

I am also facing this issue after changing the targetSdkVersion and compileSdkVersion to 29 and I think this is something to do with new privacy changes in the new Android 10. More info on the below link.

https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

I was able to solve this by manually asking the permission for ACTIVITY_RECOGNITION using permission handler package and also added below line to AndroidManifest.xml file

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

Is there any way the fitkit package can handle this?

Feduch commented 4 years ago

Hi!

I using this package, for manually asking the permission.

https://pub.dev/packages/permission_handler

Feduch commented 4 years ago

Hi!

Code how you can request access to activity on 10 android.


    if (Platform.isAndroid) {
      var androidInfo = await DeviceInfoPlugin().androidInfo;
      int sdkInt = androidInfo.version.sdkInt;

      if (sdkInt > 28) {
        bool activityRecognitionStatus =
            await Permission.activityRecognition.status.isGranted;

        if (!activityRecognitionStatus) {
          if (await Permission.activityRecognition.request().isGranted) {}
        }
      }
    }
  }`
cubuspl42 commented 3 years ago

@Feduch I think that it's really not obvious that this snippet is required for FitKit to work on Android phones! Why isn't this logic part of FitKit.requestPermissions or another helper method, let's say FitKit.requestSystemPermissions?

At very least it should be explicitly documented in the README that such snippet is required, or even included in the example code!

krokyze commented 3 years ago

@Feduch @cubuspl42 you're always welcome to make a PR with changes.