Class 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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal AsyncResource<SensorSession> connect(HealthSensor sensor, HealthSensorProfile profile, SensorSessionOptions options) Connects to a discovered sensor and starts streaming.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.final List<SensorSession> Every session currently connected or reconnecting.booleantruewhen this device can talk to BLE sensors at all -- that is, when Bluetooth LE is supported by the port and the hardware.final SensorScanstartScan(SensorScanSettings settings, SensorDiscoveryListener listener) Scans for sensors matchingsettings, reporting each discovery tolistener.
-
Constructor Details
-
HealthSensors
public HealthSensors()
-
-
Method Details
-
isSupported
public boolean isSupported()truewhen this device can talk to BLE sensors at all -- that is, when Bluetooth LE is supported by the port and the hardware. -
startScan
Scans for sensors matching
settings, reporting each discovery tolistener.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
Every session currently connected or reconnecting.
-