1

The gym is closed, but the national "exercise volume" has exploded.

The exercise self-discipline that private education could not bring back was brought back by Liu Genghong's set of shuttlecock exercises. Recently, the programmer A Qiang was also driven by his girlfriend's punch card, and followed Coach Liu to start the at-home fitness mode. The online resonates with millions of netizens at the same frequency. A Qiang said that he finally found the correct way to open the happy exercise.
Aqiang has experienced the rise of home fitness needs, and the shortcomings of BUT have gradually emerged.

Online follow-up, a set of movements, a unified rhythm cannot adapt to everyone's physical state... For students who have not exercised for a long time or have a small amount of exercise, the body that is accustomed to the peace mode suddenly accepts a wide-open, time-consuming exercise Exercise, fitness enthusiasm can keep up, but the body and heart can't keep up, so boys and girls who are exercising at home need a personal fitness butler to monitor their exercise status in real time!

After receiving the development needs sent from his heart, Aqiang did what he said. Using the sports health service capabilities of HMS Core, he created a sports steward app. After starting to exercise, real-time monitoring of exercise time, calories consumed, heart rate and other health data, Helps adjust exercise status.

Demo example

About HUAWEI Health Kit

Sports health services are divided into basic capacity services and extended capacity services.

Basic ability service : The basic ability of Health Kit provides atomic data opening. After obtaining the user's authorization for the data, the application accesses the sports health data through the interface, and performs operations such as adding, deleting, modifying, and checking the user data, so as to provide users with sports health class data service.

Expansion capability service : The Health Kit expansion capability service opens up more real-time exercise and health data, and scenario-based data for exercise and health solutions.

The related capabilities of the Sports Manager App are developed based on the functions in the expansion capabilities of the Health Kit.

Development Process

1. Development environment requirements

Requirements for the Android platform

Install Android Studio 3.X and above

JDK 1.8.211 and above

The application should meet the following conditions

minSdkVersion 24

targetSdkVersion 29

compileSdkVersion 29

Gradle 4.6 and above

The device for testing the app requires a Huawei mobile phone with Android 6.0 or above and the Huawei Sports Health App installed.

2. Development steps

The complete development process is as follows.

3. Realization of start and stop motion functions

The order of use of controlling motion and acquiring real-time motion is generally as follows:

(1) registerSportData, start to obtain real-time sports data.

(2) startSport, start sports.

(3) stopSport, stop the movement.

(4) unregisterSportData, stop acquiring real-time sports data.

key code steps

(1) Start acquiring real-time motion data

  1. Call the registerSportData method of the HiHealthDataStore object to start acquiring real-time sports data.
  2. Through the request parameter HiSportDataCallback object, the query result is returned, and the data type in the result refers to the key value of the real-time sports Bundle object.
 HiHealthDataStore.registerSportData(context, new HiSportDataCallback() {
    @Override
    public void onResult(int resultCode) {
        // 接口调用结果 
        Log.i(TAG, "registerSportData onResult resultCode:" + resultCode);
    }

    @Override
    public void onDataChanged(int state, Bundle bundle) {
        // 实时数据变化回调        
        Log.i(TAG, "registerSportData onChange state: " + state);        
        StringBuffer stringBuffer = new StringBuffer("");              
        if (state == HiHealthKitConstant.SPORT_STATUS_RUNNING) {
            Log.i(TAG, "heart rate : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_HEARTRATE));
            Log.i(TAG, "distance : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_DISTANCE));
            Log.i(TAG, "duration : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_DURATION));
            Log.i(TAG, "calorie : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_CALORIE));
            Log.i(TAG, "totalSteps : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_TOTAL_STEPS));
            Log.i(TAG, "totalCreep : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_TOTAL_CREEP));
            Log.i(TAG, "totalDescent : " + bundle.getInt(HiHealthKitConstant.BUNDLE_KEY_TOTAL_DESCENT));
        }        
    }
});

(2) start exercising

Motion type constants supported by start and end motions

  1. Call the startSport method of the HiHealthDataStore object to start the corresponding type of exercise.
  2. The query result is returned through the request parameter ResultCallback object.
 // 室外跑步
int sportType = HiHealthKitConstant.SPORT_TYPE_RUN;
HiHealthDataStore.startSport(context, sportType, new ResultCallback() {
    @Override
    public void onResult(int resultCode, Object message) {
        if (resultCode == HiHealthError.SUCCESS) {
            Log.i(TAG, "start sport success");
        }
    }
});

(3) End the exercise:

  1. Call the stopSport method of the HiHealthDataStore object to stop the corresponding type of movement.
  2. The query result is returned through the request parameter ResultCallback object.
 HiHealthDataStore.stopSport(context, new ResultCallback() {
    @Override
    public void onResult(int resultCode, Object message) {
        if (resultCode == HiHealthError.SUCCESS) {
            Log.i(TAG, "stop sport success");
        }
    }
});

(4) Stop acquiring real-time motion data:

  1. Call the unregisterSportData method of the HiHealthDataStore object to stop acquiring real-time sports data.
  2. Return the query result through the request parameter HiSportDataCallback object.
 HiHealthDataStore.unregisterSportData(context, new HiSportDataCallback() {
    @Override
    public void onResult(int resultCode) {
        // 接口调用结果
        Log.i(TAG, "unregisterSportData onResult resultCode:" + resultCode);
    }

    @Override
    public void onDataChanged(int state, Bundle bundle) {
        // 此时不会被调用
    }
});

4. Implementation of the query function of today's activity

Query the daily activity volume, including step count, step details, distance, calories, and high-intensity exercise. This part of the data comes from mobile phones or Huawei wearable devices. You need to apply for activation permission and obtain user authorization. Otherwise, the interface will fail to call. . Click here for required permissions.

(1) Use the execQuery interface to query the daily activity of the user

1. Call the execQuery method of the HiHealthDataStore object to query the daily activity of the user.

2. Return the query result through the request parameter ResultCallback object.

Step count as an example:

 int timeout = 0;
// 查询当天步数
Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
long startTime = currentDate.getTimeInMillis();
long endTime = System.currentTimeMillis();
// 查询步数
HiHealthDataQuery hiHealthDataQuery = new HiHealthDataQuery(HiHealthPointType.DATA_POINT_STEP_SUM, startTime,
        endTime, new HiHealthDataQueryOption());
HiHealthDataStore.execQuery(context, hiHealthDataQuery, timeout, new ResultCallback() {
    @Override
    public void onResult(int resultCode, Object data) {
        Log.i(TAG, "query steps resultCode: " + resultCode);
        if (resultCode == HiHealthError.SUCCESS && data instanceof List) {
            List dataList = (ArrayList) data;
            for (Object obj : dataList) {
                HiHealthPointData pointData = (HiHealthPointData) obj;
                Log.i(TAG, "start time : " + pointData.getStartTime());
                Log.i(TAG, "query steps : " + String.valueOf(pointData.getValue()));
            }
        }
    }
});

Parameters required to query data and query results:

5. Exercise record query

Code example for querying exercise records within 30 days:

1. Call the execQuery method of the HiHealthDataStore object to query the user's exercise records.

2. Return the query result through the request parameter ResultCallback object.

 int timeout = 0;
 long endTime = System.currentTimeMillis();
 // 查询时间范围 : 30 天
 long startTime = endTime - 1000 * 60 * 60 * 24 * 30L;
 // 查询跑步记录
 HiHealthDataQuery hiHealthDataQuery = new HiHealthDataQuery(HiHealthSetType.DATA_SET_RUN_METADATA, startTime,
         endTime, new HiHealthDataQueryOption());
 HiHealthDataStore.execQuery(context, hiHealthDataQuery, timeout, new ResultCallback() {
     @Override
     public void onResult(int resultCode, Object data) {
if (resultCode == HiHealthError.SUCCESS && data instanceof List){ 
            List dataList = (List) data;
            for (Object obj : dataList) {
                HiHealthSetData hiHealthData = (HiHealthSetData) obj;
                Map map = hiHealthData.getMap();
                Log.i(TAG, "start time : " + hiHealthData.getStartTime());
                Log.i(TAG, "total_time : " +  map.get(HiHealthKitConstant.BUNDLE_KEY_TOTAL_TIME));
                Log.i(TAG, "total_distance : " + map.get(HiHealthKitConstant.BUNDLE_KEY_TOTAL_DISTANCE));
                Log.i(TAG, "total_calories : " + map.get(HiHealthKitConstant.BUNDLE_KEY_TOTAL_CALORIES));
                Log.i(TAG, "step : " + map.get(HiHealthKitConstant.BUNDLE_KEY_STEP));
                Log.i(TAG, "average_pace : " + map.get(HiHealthKitConstant.BUNDLE_KEY_AVERAGEPACE));
                Log.i(TAG, "average_speed : " + map.get(HiHealthKitConstant.BUNDLE_KEY_AVERAGE_SPEED));
                Log.i(TAG, "average_step_rate : " + map.get(HiHealthKitConstant.BUNDLE_KEY_AVERAGE_STEP_RATE));
                Log.i(TAG, "step_distance : " + map.get(HiHealthKitConstant.BUNDLE_KEY_STEP_DISTANCE));
                Log.i(TAG, "average_heart_rate : " + map.get(HiHealthKitConstant.BUNDLE_KEY_AVERAGE_HEART_RATE));
                Log.i(TAG, "total_altitude : " + map.get(HiHealthKitConstant.BUNDLE_KEY_TOTAL_ALTITUDE));
                Log.i(TAG, "total_descent : " + map.get(HiHealthKitConstant.BUNDLE_KEY_TOTALDESCENT));
                Log.i(TAG, "data source : " + map.get(HiHealthKitConstant.BUNDLE_KEY_DATA_SOURCE));
            }
        }
     }
 });

Click here to query the parameters required for data and query results

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙应用,和开发者共同成长。