matinzd/react-native-health-connect

## ๐Ÿž Bug Report: IllegalStateException in Health Connect SDK

Closed this issue ยท 1 comments

Describe the bug
While testing for SDK availability, an IllegalStateException occurs when attempting to check or initialize the Health Connect SDK. The app crashes unexpectedly instead of handling the error gracefully.

To Reproduce
Steps to reproduce the behavior:

  1. Attempt to use authHealthConnect function when Health Connect is not installed or needs an update.
  2. Observe the crash with the following error message
    androidx.health.platform.client.impl.ipc.internal.ServiceConnection in connect

Expected behavior
When the Health Connect SDK is unavailable, a popup should appear informing the user to download or update Health Connect. The app should not crash.

Code

export const authHealthConnect = async e => {
  try {
    const status = await getSdkStatus();
    if (
      status === SdkAvailabilityStatus.SDK_UNAVAILABLE ||
      status === SdkAvailabilityStatus.SDK_UNAVAILABLE_PROVIDER_UPDATE_REQUIRED
    ) {
      Alert.alert(
        'Health Connect Not Available',
        'Please check if Health Connect is installed.',
        [
          {
            text: 'Download Health Connect',
            onPress: () =>
              Linking.openURL(
                `market://details?id=${HEALTH_APP_BUNDLE_ID.HEALTH_CONNECT}`,
              ),
          },
          {text: 'OK'},
        ],
      );
      throw {
        success: false,
        sdk_unavilable: true,
        message:
          status === SdkAvailabilityStatus.SDK_UNAVAILABLE
            ? 'SDK is not available'
            : SdkAvailabilityStatus.SDK_UNAVAILABLE_PROVIDER_UPDATE_REQUIRED
            ? 'SDK is not available, provider update required'
            : '',
      };
    }
    await initialize();
    await requestPermission(
      options?.scopes?.map(recordType => {
        return {accessType: 'read', recordType};
      }),
    );
    const permissions = await getGrantedPermissions();
    if (permissions?.length) {
      return {
        success: true,
        permissions,
      };
    }
    throw {
      success: false,
    };
  } catch (error) {
    revokePermissionsHealthConnect()
    return {
      success: false,
      error,
    };
  }
};

Screenshots
image

Environment:

  • Health Connect Version:"^2.1.0"
  • React Native Version: "0.74.3"
  • New architecture enabled: Yes/No
  • Using Expo: No
  • Android API Level: Android 13

You might not have health connect installed on your device. Make sure it's installed. Please reopen if problem persists.