public class HealthSensors

  1. Object
  2. HealthSensors

Discovers and streams from standard Bluetooth SIG health sensors – heart-rate straps, power meters, speed and cadence sensors, foot pods, thermometers, scales, blood-pressure cuffs and glucose meters.

Obtain one from Health.getSensors().

This works everywhere Bluetooth LE does

Unlike the rest of the health API, this layer needs no platform health store and no per-port implementation: it is built entirely on com.codename1.bluetooth.le, so it behaves identically on iOS, Android, the simulator, the desktop ports and – where the browser exposes Web Bluetooth – JavaScript. A desktop app that reports HealthAvailability.LOCAL_ONLY for the store can still stream a chest strap.

Quick start

HealthSensors sensors = Health.getInstance().getSensors();
SensorScanSettings settings = new SensorScanSettings()
        .addProfile(HealthSensorProfile.HEART_RATE)
        .setTimeoutMillis(15000);
SensorScan scan = sensors.startScan(settings, new SensorDiscoveryListener() {
    public void sensorDiscovered(HealthSensor sensor) {
        scan.stop();
        sensors.connect(sensor, HealthSensorProfile.HEART_RATE,
                new SensorSessionOptions())
              .onResult((session, err) -> { ... });
    }
    public void scanFailed(HealthException e) { Log.e(e); }
});

Permissions

These are Bluetooth operations, not health-store operations, so they need Bluetooth permission – BluetoothPermission.SCAN and CONNECT – and not a HealthKit entitlement or a Health Connect declaration. The build server makes the same distinction: an app that only uses this package is not treated as a health-data app.

Constructors

public HealthSensors()

Methods

public boolean isSupported()true when this device can talk to BLE sensors at all – that is, when Bluetooth LE is supported by the port and the hardware.
public final SensorScan startScan(SensorScanSettings settings, SensorDiscoveryListener listener)Scans for sensors matching settings, reporting each discovery to listener.
public final AsyncResource<SensorSession> connect(HealthSensor sensor, HealthSensorProfile profile, SensorSessionOptions options)Connects to a discovered sensor and starts streaming.
public final AsyncResource<SensorSession> connect(String sensorId, HealthSensorProfile profile, SensorSessionOptions options)Reconnects to a sensor by the identifier persisted from an earlier session, without scanning first.
public final List<SensorSession> getActiveSessions()Every session currently connected or reconnecting.

Inherited methods

Constructor details

HealthSensors

public HealthSensors()

Method details

isSupported

public boolean isSupported()
true when this device can talk to BLE sensors at all – that is, when Bluetooth LE is supported by the port and the hardware.

startScan

public final SensorScan startScan(SensorScanSettings settings, SensorDiscoveryListener listener)

Scans for sensors matching settings, reporting each discovery to listener.

Returns a handle even when scanning cannot start; the failure arrives through SensorDiscoveryListener.scanFailed(HealthException) rather than as a null return, so callers never need a null check.

connect

public final AsyncResource<SensorSession> connect(HealthSensor sensor, HealthSensorProfile profile, SensorSessionOptions options)
Connects to a discovered sensor and starts streaming.

connect

public final AsyncResource<SensorSession> connect(String sensorId, HealthSensorProfile profile, SensorSessionOptions options)

Reconnects to a sensor by the identifier persisted from an earlier session, without scanning first.

This is how a fitness app should reconnect to the user’s own strap: remember HealthSensor.getId() the first time and go straight to it afterwards, rather than making them pick from a list every session.

getActiveSessions

public final List<SensorSession> getActiveSessions()
Every session currently connected or reconnecting.