Class Health
Entry point for the Codename One health API -- reading and writing health data, watching it for changes, recording workouts, and streaming from Bluetooth health sensors.
Obtain the platform implementation via getInstance(); the returned
object is owned by the active port and is never null.
The API is split by role:
getStore()-- the platform health store: samples, aggregates, writes and change subscriptions.getWorkouts()-- live and recorded workout sessions.getSensors()-- standard Bluetooth GATT health sensors: heart-rate straps, power meters, scales, blood-pressure cuffs, glucose meters.
Quick start
Health health = Health.getInstance();
if (health.getAvailability() != HealthAvailability.AVAILABLE) {
health.openProviderSetup();
return;
}
HealthStore store = health.getStore();
store.requestAuthorization(HealthAccess.read(HealthDataType.STEPS))
.onResult((asked, err) -> {
if (err == null) {
readTodaysSteps(store);
}
});
Two things that will surprise you
You cannot ask whether you may read. HealthKit deliberately refuses
to disclose read authorization -- a denied read looks exactly like an
empty result. So requestAuthorization resolving true means the user
was asked, not that they agreed, and an empty query result means
"denied or no data" with no way to tell which. Never render that to a
user as "you denied access"; say "no data available". See
HealthStore.getReadAuthorizationStatus(HealthDataType).
Android never wakes your app for new data. Health Connect has no
push mechanism, so subscriptions there are drained when your app runs.
Check HealthSubscription.isPushDelivery() rather than assuming.
Threading
Every method may be called from the EDT and returns immediately; every
callback is delivered on the EDT. The one qualification is
HealthBackgroundListener, which may run with no visible UI after the
OS relaunched your app in the background.
Platform support
- iOS / watchOS -- HealthKit, including background delivery and (on watchOS, and on iOS 26 and later) live workout sessions. Requires the HealthKit entitlement and privacy usage strings; the build fails with an actionable message if they are missing.
- Android -- Health Connect, including exercise sessions. Requires the provider app, a privacy-policy declaration and per-type permissions declared through build hints.
- JavaSE simulator -- a scriptable virtual health store (Simulate -> Health Simulation) with synthetic data, permission scripting and fault injection.
- Desktop and JavaScript -- no platform store exists, so these
report
HealthAvailability.LOCAL_ONLYand keep data locally. The sensor API works fully wherever Bluetooth LE does. - tvOS and all other ports -- this base class is returned as-is and
reports health as unsupported; operations fail fast with
HealthError.NOT_SUPPORTED.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWhether a health store is usable right now, and if not, why.Build-configuration problems detected at runtime -- a missingios.NSHealthShareUsageDescriptionbuild hint, an absent Health Connect privacy-policy declaration.static HealthReturns the platform-specific singleton owned by the current port.Bluetooth health sensors.getStore()The platform health store.Workout recording.booleantruewhen this port exposes health data in any form.Opens the operating system screen where the user can change health permissions -- Settings on iOS, the Health Connect permission screen on Android.Sends the user to install or update the Health Connect provider.
-
Constructor Details
-
Health
protected Health()Ports construct subclasses. Application code obtains the active instance viagetInstance().
-
-
Method Details
-
getInstance
-
isSupported
public boolean isSupported()truewhen this port exposes health data in any form.falseon the fallback base class. -
getAvailability
Whether a health store is usable right now, and if not, why. Check this before anything else -- on Android the provider app may be missing or out of date, which the user can fix. -
getStore
The platform health store. Never null: on ports without one this returns a no-op store whose every operation fails withHealthError.NOT_SUPPORTED. Branch onHealthStore.isSupported(). -
getWorkouts
Workout recording. Never null. -
getSensors
Bluetooth health sensors. Never null, and -- unlike the other two -- not a no-op on ports without a health store: the sensor layer is built entirely oncom.codename1.bluetooth.le, so it works anywhere Bluetooth LE does, including the desktop and JavaScript ports. -
openHealthSettings
Opens the operating system screen where the user can change health permissions -- Settings on iOS, the Health Connect permission screen on Android. Resolves
falsewhere no such screen exists.This is the right response to a query that came back empty when you expected data, since on iOS you cannot tell denial from absence.
-
openProviderSetup
Sends the user to install or update the Health Connect provider. Meaningful whengetAvailability()isHealthAvailability.PROVIDER_NOT_INSTALLEDorHealthAvailability.PROVIDER_UPDATE_REQUIRED; resolvesfalseelsewhere. -
getConfigurationProblems
Build-configuration problems detected at runtime -- a missing
ios.NSHealthShareUsageDescriptionbuild hint, an absent Health Connect privacy-policy declaration. Empty when the app is configured correctly.Operations that need a missing entry throw
HealthConfigurationExceptioncarrying the same text. This method reports the same diagnostics without throwing, so a diagnostics screen or a test can assert on them.
-