Menu

 

React Native

React Native library for integrating WeFitter and Samsung Health into your app

  • This library is written with Native Modules for React Native CLI. Expo Go is unsupported

Installation

yarn add https://github.com/wefitter/react-native-wefitter-samsung.git#v2.1.2

 

Add wefitter-shealth-2.1.2.aar and samsung-health-data-1.5.0.aar to libs folder which should normally be in app/libs. These files can be found in node_modules/react-native-wefitter-samsung/android/libs in your protect or here. Make sure you select the same version tag you are using in your app before downloading to prevent incompatibility issues.

 

Usage

Add the following code:

Breaking changes:

1. You can specify your app's permissions as shown in the example to configure the WeFitter SDK. By doing this, the user only sees the permissions for which your app has been approved. If you do not specify these permissions, ALL permissions will be presented to the user, thus also permissions for which you app has not been approved.

2. You have to request the  android.Manifest.permission.ACTIVITY_RECOGNITION permission before you connect the WeFitter SDK, as without this permission the WeFitter SDK will never connect.

See the example app for the full source.

Show code example
Hide code example
import WeFitterSamsung, {
  ConfiguredEvent,
  ConnectedEvent,
  ErrorEvent,
} from 'react-native-wefitter-samsung';

// ...

const [connected, setConnected] = useState<boolean>(false);
const prefix = 'com.samsung.health';
// Setup your Samsung approved permissions here
// The user will see these permissions only in the permission selection screen
const myAppPermissions: string[] = [
  `com.samsung.shealth.step_daily_trend`,
  `${prefix}.heart_rate`,
  `${prefix}.height`,
  `${prefix}.weight`,
  `${prefix}.sleep_stage`,
  `${prefix}.exercise`,
  //"$prefix.blood_glucose",
  //"$prefix.blood_pressure",
  //"$prefix.body_fat",
  //"$prefix.body_temperature",
  //"$prefix.oxygen_saturation",'
];
const myAppPermissionsString = myAppPermissions.join(',');

useEffect(() => {
  if (Platform.OS === 'android') {
    // create native event emitter and event listeners to handle status updates
    const emitter = new NativeEventEmitter();
    const configuredListener = emitter.addListener(
      'onConfiguredWeFitterSamsung',
      (event: ConfiguredEvent) =>
        console.log(`WeFitterSamsung configured: ${event.configured}`)
    );
    const connectedListener = emitter.addListener(
      'onConnectedWeFitterSamsung',
      (event: ConnectedEvent) => {
        console.log(`WeFitterSamsung connected: ${event.connected}`);
        setConnected(event.connected);
      }
    );
    const errorListener = emitter.addListener(
      'onErrorWeFitterSamsung',
      (event: ErrorEvent) => {
        console.log(`WeFitterSamsung error: ${event.error}`);

        // `error` can be checked to override specific messages
        // `forUser` boolean indicates the error requires user interaction
        if (event.forUser) {
          Alert.alert('', event.error, [
            {
              text: 'OK',
              onPress: () => {
                // `tryToResolveError` will guide the user on fixing certain errors
                // for example:
                // - link to the app store to install or update Samsung Health
                // - open Samsung Health to accept privacy policy
                // - open Settings when Samsung Health needs to be enabled
                WeFitterSamsung.tryToResolveError(event.error);
              },
            },
          ]);
        }
      }
    );

    // create config
    const config = {
      token: 'YOUR_TOKEN', // required
      apiUrl: 'YOUR_API_URL', // required, the url should be base without `v1/ingest/` as the library will append this. For example: `https://api.wefitter.com/api/`
      startDate: 'CUSTOM_START_DATE', // optional with format `yyyy-MM-dd`, by default data of the past 7 days will be uploaded
      notificationTitle: 'CUSTOM_TITLE', // optional
      notificationText: 'CUSTOM_TEXT', // optional
      notificationIcon: 'CUSTOM_ICON', // optional, e.g. `ic_notification` placed in either drawable, mipmap or raw
      notificationChannelId: 'CUSTOM_CHANNEL_ID', // optional
      notificationChannelName: 'CUSTOM_CHANNEL_NAME', // optional
      myAppPermissions: myAppPermissionsString,
    };

    // configure WeFitterSamsung
    WeFitterSamsung.configure(config);

    return () => {
      configuredListener.remove();
      connectedListener.remove();
      errorListener.remove();
    };
  }
  return;
}, []);

const onPressConnectOrDisconnect = () => {
  if (Platform.OS === 'android') {
    connected ? WeFitterSamsung.disconnect() : WeFitterSamsung.connect();
  } else {
    Alert.alert('Not supported', 'WeFitterSamsung is not supported on iOS');
  }
};

Thank you for reaching out!

We will be in touch with you soon. WeFitter team.

You are subscribed!

You will soon receive our newsletter!
WeFitter team.