admin管理员组

文章数量:1296300

I'm trying to read the number of steps in the last day on Wear OS using Java. My goal is to reward the user if they achieve a certain number of steps.

To achieve this I tried using HealthServices. I followed this guide from the official Android Developers website. I added the required ACTIVITY_RECOGNITION permission to my AndroidManifest file and requested it at runtime in my Java code.

The problem is that when I try to get the list of supported capabilities (data types) I get a set which only contains one DataType, heart rate. I tested both on an Wear OS emulator (API 34) and physical Wear OS device (Galaxy Watch 5 Pro). When testing on the emulator I updated to the latest version of Android Studio which has the Wear Health Services option for Wear OS devices. I enabled all available capabilities and still nothing changed.

What could be causing the problem? Is this even the correct approach to getting the number of steps in the last day?

    public void setupMeasureClient() {
        measureClient = HealthServices.getClient(context).getMeasureClient();
    }
    public void loadSupportedDataTypes() {
        if (measureClient == null) return;

        if (!permissionHandler.hasPermission(ACTIVITY_RECOGNITION_PERMISSION)) return;

        try {
            ListenableFuture<MeasureCapabilities> future = measureClient.getCapabilitiesAsync();
            future.addListener(() -> {
                try {
                    MeasureCapabilities measureCapabilities = future.get();
                    Set<DeltaDataType<?, ?>> dataTypes =  measureCapabilities.getSupportedDataTypesMeasure();
                    System.out.println("[HealthServices] Available data types:");
                    for (final DeltaDataType<?, ?> dataType: dataTypes) {
                        System.out.println("[HealthServices] Data type: " + dataType.getName());
                    }
                    setDataTypes(dataTypes);
                } catch (Exception e) {
                    System.err.println("[HealthServices] Error occurred while resolving future: " + e.getMessage());
                }
            }, Executors.newSingleThreadExecutor());
        } catch (Exception e) {
            System.out.println("[HealthServices] Couldn't get list of capabilities: " + e.getMessage());
        }
    }

I'm trying to read the number of steps in the last day on Wear OS using Java. My goal is to reward the user if they achieve a certain number of steps.

To achieve this I tried using HealthServices. I followed this guide from the official Android Developers website. I added the required ACTIVITY_RECOGNITION permission to my AndroidManifest file and requested it at runtime in my Java code.

The problem is that when I try to get the list of supported capabilities (data types) I get a set which only contains one DataType, heart rate. I tested both on an Wear OS emulator (API 34) and physical Wear OS device (Galaxy Watch 5 Pro). When testing on the emulator I updated to the latest version of Android Studio which has the Wear Health Services option for Wear OS devices. I enabled all available capabilities and still nothing changed.

What could be causing the problem? Is this even the correct approach to getting the number of steps in the last day?

    public void setupMeasureClient() {
        measureClient = HealthServices.getClient(context).getMeasureClient();
    }
    public void loadSupportedDataTypes() {
        if (measureClient == null) return;

        if (!permissionHandler.hasPermission(ACTIVITY_RECOGNITION_PERMISSION)) return;

        try {
            ListenableFuture<MeasureCapabilities> future = measureClient.getCapabilitiesAsync();
            future.addListener(() -> {
                try {
                    MeasureCapabilities measureCapabilities = future.get();
                    Set<DeltaDataType<?, ?>> dataTypes =  measureCapabilities.getSupportedDataTypesMeasure();
                    System.out.println("[HealthServices] Available data types:");
                    for (final DeltaDataType<?, ?> dataType: dataTypes) {
                        System.out.println("[HealthServices] Data type: " + dataType.getName());
                    }
                    setDataTypes(dataTypes);
                } catch (Exception e) {
                    System.err.println("[HealthServices] Error occurred while resolving future: " + e.getMessage());
                }
            }, Executors.newSingleThreadExecutor());
        } catch (Exception e) {
            System.out.println("[HealthServices] Couldn't get list of capabilities: " + e.getMessage());
        }
    }
Share Improve this question asked Feb 11 at 21:26 AndrejAndrej 3,2353 gold badges19 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

MeasureClient is for use cases that need brief access to the sensors. A common scenario might be showing the user their Heart Rate in the moment.

PassiveMonitoringClient is well-suited for your use case. This will allow you to receive updates about the user's daily metrics like steps, distance, and floors climbed. It might also be helpful to take a look at this sample, which demonstrates tracking daily steps.

本文标签: javaWear OS HealthServices only return HeartRate when requesting supported capabilitiesStack Overflow