The hot summer has finally passed, and many people have started a new round of fitness programs with the cool autumn wind. When users do outdoor sports or use equipment such as treadmills and elliptical machines, they will want to start the exercise and record exercise data with a click in the sports and health app. For apps developed by developers themselves, users can directly control the user's motion status and obtain real-time data in the background of their own apps when they exercise using the sports equipment bound to the Huawei Health App, without needing to access the Huawei Health App. to operate.
So, for the sports health app, how to realize the above functions? The extended capability service of HMS Core sports health service opens up more real-time sports and health data, and scenario-based data of sports and health solutions. The ability to control exercise and obtain real-time exercise data provides interfaces for starting, pausing, resuming, and ending exercise. Developers can directly call the interface in the app and control the corresponding exercise state in the Sports Health App in the background without jumping to the Huawei Sports Health App. The exercise interface is operated. At this time, the exercise health app will not pop up the exercise page, but will be executed in the background.
At the same time, Huawei also provides interfaces to obtain real-time motion data and stop acquiring real-time motion data. To prevent data loss, generally call the acquire real-time motion data interface before starting exercise, and call the stop acquiring real-time motion data interface after stopping the exercise. If the user binds a Huawei wearable device, the wearable device will automatically enter the exercise interface when starting the exercise; when the exercise is ended, the wearable device will automatically end the exercise. Before using the interface, you need to apply for the activation permission from Huawei and obtain user authorization. Otherwise, the interface call will fail. Sports types currently supported: outdoor walking, outdoor running, outdoor cycling, indoor running (treadmill), elliptical machine, rowing machine, indoor cycling. For the data type obtained in the specific scene, please refer to the key value of the real-time motion Bundle object .
The foreground motion jumps to the device pairing page
Demo
Development steps
development preparation
1. Apply for Health Kit service
Before applying for Health Kit service , please complete the application account service.
2. Integrate HMS Core SDK
Before integrating the SDK , please integrate the HUAWEI ID Services SDK .
Before starting development, please integrate the SDK into the Android Studio development environment. Android Studio should be V3.3.2 and above.
Development steps
1. Start getting real-time motion data
- Call the registerSportData method of the HiHealthDataStore object to start acquiring real-time sports data.
- 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 .
Sample code:
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. Stop getting real-time exercise data
- Call the unregisterSportData method of the HiHealthDataStore object to stop acquiring real-time sports data.
- Return the query result through the request parameter HiSportDataCallback object.
Sample code:
HiHealthDataStore.unregisterSportData(context, new HiSportDataCallback() {
JSONObject jsonResult
@Override
public void onResult(int resultCode) {
// 接口调用结果
Log.i(TAG, "unregisterSportData onResult resultCode:" + resultCode);
}
@Override
public void onDataChanged(int state, Bundle bundle) {
// 此时不会被调用
}
});
3. Start exercising according to the type of exercise
- Call the startSport method of the HiHealthDataStore object to start the corresponding type of exercise.
- Returns the query result through the request parameter ResultCallback object.
Sample code:
// 室外跑步
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");
}
}
});
- For equipment exercise (such as treadmill exercise, rowing machine exercise, elliptical machine exercise, and spinning exercise), it is necessary to distinguish between scenarios with or without paired devices. For example, if the user wants to start rowing machine exercise:
At this time, the Sports Health App has been paired with a rowing machine, then the paired device is connected by default, and then background exercise is enabled.
At this time, the Sports Health App is paired with more than one rowing machine, then a pop-up box will be displayed to select the device, click to start exercise, return to the application page, and then start the background exercise.
At this time, the Sports Health App does not pair the rowing machine device, then it will jump to the one-click scanning page of the Sports Health App to pair the rowing machine device, as shown in the figure below, after the pairing is successful, it will return to the application page, and then start the background exercise.
4. Start exercising according to the device information
- Call the startSportEx method of the HiHealthDataStore object, and pass in the corresponding startup parameter StartSportParam. You can set the parameter CharacteristicConstant.SportModeType to control whether the foreground or background starts sports.
- Through the request parameter ResultCallback object, return the result of opening the motion state.
Sample code:
// 划船机为例
// Mac地址,连接符为":",例:"11:22:33:44:55:66"
String macAddress = "11:22:33:44:55:66" ;
// 是否支持FTMP,0 不支持,1 支持
int isSupportedFtmp = CharacteristicConstant.FtmpType.FTMP_SUPPORTED.getFtmpTypeValue();
// 设备类型,划船机
int deviceType = CharacteristicConstant.DeviceType.TYPE_ROWER_INDEX.getDeviceTypeValue();
// 运动类型,划船机
int sportType = CharacteristicConstant.EnhanceSportType.SPORT_TYPE_ROW_MACHINE.getEnhanceSportTypeValue();
// 构造启动参数,用于连接设备及控制运动
StartSportParam param = new StartSportParam(macAddress, isSupportedFtmp, deviceType, sportType);
// 启动方式,0 前台,1 后台
param.putInt(HiHealthDataKey.IS_BACKGROUND,
CharacteristicConstant.SportModeType.BACKGROUND_SPORT_MODE.getType());
HiHealthDataStore.startSportEx(mContext, param, new ResultCallback() {
@Override
public void onResult(int resultCode, Object message) {
if (resultCode == HiHealthError.SUCCESS) {
Log.i(TAG, "start sportEx success");
}
}
});
5. End the exercise
- Call the stopSport method of the HiHealthDataStore object to stop the corresponding type of movement (the movement opened in the foreground cannot be stopped by the stopSport method).
- Returns the query result through the request parameter ResultCallback object.
Sample code:
HiHealthDataStore.stopSport(context, new ResultCallback() {
@Override
public void onResult(int resultCode, Object message) {
if (resultCode == HiHealthError.SUCCESS) {
Log.i(TAG, "stop sport success");
}
}
});
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~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。