Menu

 

React Native

React Native library for integrating WeFitter and Health Connect 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-health-connect.git#v0.0.1

 

Add wefitter-health-connect-0.0.1.aar to libs folder which should normally be in app/libs. These files can be found in node_modules/react-native-wefitter-health-connect/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:

See the example app for the full source.

Show code example
Hide code example

import WeFitterHealthConnect, {
  ConfiguredEvent,
  ConnectedEvent,
  ErrorEvent,
} from 'react-native-wefitter-health-connect';

// ...

const [connected, setConnected] = useState<boolean>(false);

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

    // create config
    const config = {
      token: 'YOUR_TOKEN', // required, WeFitter API profile bearer token
      apiUrl: 'YOUR_API_URL', // optional, only use if you want to use your backend as a proxy and forward all API calls to the WeFitter API. Default: `https://api.wefitter.com/api/`
      startDate: 'CUSTOM_START_DATE', // optional with format `yyyy-MM-dd`, by default data of the past 20 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
    };

    // configure WeFitterHealthConnect
    WeFitterHealthConnect.configure(config);

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

const onPressConnectOrDisconnect = () => {
  if (Platform.OS === 'android') {
    WeFitterHealthConnect.isSupported((supported) => {
      if (supported) {
        connected
          ? WeFitterHealthConnect.disconnect()
          : WeFitterHealthConnect.connect();
      } else {
        Alert.alert(
          'Not supported',
          'WeFitterHealthConnect is not supported on this device'
        );
      }
    });
  } else {
    Alert.alert(
      'Not supported',
      'WeFitterHealthConnect 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.