Connecting with Health Platform

Connect to Health Platform with:

  • HealthTrackingService.connectService()
import com.samsung.android.service.health.tracking.HealthTrackingService;
import com.samsung.android.service.health.tracking.ConnectionListener;
import com.samsung.android.service.health.tracking.data.HealthTrackerType;

public class SubActivity extends FragmentActivity {

    public HealthTrackingService healthTracking;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Connect to Health Platform.
        healthTracking = new HealthTrackingService(connectionListener, this);
        healthTracking.connectService();

        // …
    }

    @Override
    protected void onDestroy() {
        // Disconnect the tracking service.
        healthTracking.disconnectService();
    }

Implement a tracker event listener with:

  • HealthTracker.TrackerEventListener

If the connection failed, check HealthTrackerException. If the exception has a resolution, resolve it by calling:

  • HealthTrackerException.resolve()
    private final connectionListener = new ConnectionListener() {

        @Override
        public void onConnectionSuccess() {
            // Connection success.

            // Capability Check
            // Tracking Data
        }

        @Override
        public void onConnectionEnded() {
            // Connection is ended.
        }

        @Override
        public void onConnectionFailed(HealthTrackerException e) {
            // Connection failed. Check the error and resolve.
            if (e.hasResolution()) {
                e.resolve(SubActivity.this);
            }
        }

    };

If the connection fails, check the error and resolve it with:

  • HealthTrackerException.getErrorCode()
  • HealthTrackerException.hasResolution()
  • HealthTrackerException.resolve()