This interface helps to get a connection event with Health Platform.
The
ConnectionListener is set in a constructor of
HealthTrackingService. A connection result is retrieved in:
public class MainActivity extends Activity {
private HealthTrackingService healthTrackingService = null;
private HealthTracker ecgTracker = null;
private final ConnectionListener connectionListener = new ConnectionListener() {
@Override
public void onConnectionSuccess() {
// Connection with Health Platform succeeded. Check capability.
List<HealthTrackerType> availableTrackers = healthTrackingService.getTrackingCapability().getSupportHealthTrackerTypes();
if (!availableTrackers.contains(HealthTrackerType.ECG_ON_DEMAND)) {
// Tracking Electrocardiogram (ECG) On-Demand is not available.
// Display a proper message to the user.
} else {
ecgTracker = healthTrackingService.getHealthTracker(HealthTrackerType.ECG_ON_DEMAND);
}
}
@Override
public void onConnectionEnded() {
// The connection with Health Platform was ended.
}
@Override
public void onConnectionFailed(HealthTrackerException exception) {
//Check the error code and resolve it.
if (exception.hasResolution()) {
// Display a proper message before calling resolve().
exception.resolve(MainActivity.this); // Jumping to an application market to install or update Health Platform.
} else {
// Display a proper message for the exception.
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// Check and request permission.
if (ActivityCompat.checkSelfPermission(getApplicationContext(), "android.permission.BODY_SENSORS")
== PackageManager.PERMISSION_DENIED) {
requestPermissions(this, new String[]{Manifest.permission.BODY_SENSORS}, 0);
}
try {
// Connect the Health Tracking Service.
healthTrackingService = new HealthTrackingService(connectionListener, context);
healthTrackingService.connectService();
} catch (Throwable t) {
// Exception handling.
}
}
}