为什么我在华为Mate 30 Pro上测试集成了HMS Health Kit的应用,获取不到步数?
When I test an app integrated with the HMS Health Kit on my Huawei Mate 30 Pro, why cannot I obtain the number of steps?
使用DataController
下对应方法查询步数返回错误码50005
怎么办? (我在华为开发者网站申请的Health Kit scope已经通过)
2020-05-26 11:41:21.195 17338-17338/com.hauwei.hmsdemo I/DataManager:
read failure 50005:Unknown authorization error
2020-05-26 11:41:21.203 17338-17338/com.hauwei.hmsdemo I/DataManager:
以下几行用于查询步数:
public void readSteps(View view) throws ParseException {
DataCollector dataCollector = new DataCollector.Builder().setPackageName(context)
.setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
.setDataStreamName("STEPS_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate = dateFormat.parse(BEGIN_TIME);
Date endDate = dateFormat.parse(END_TIME);
ReadOptions readOptions = new ReadOptions.Builder().read(dataCollector)
.setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
.build();
dataController.read(readOptions).addOnSuccessListener(new OnSuccessListener<ReadReply>() {
@Override
public void onSuccess(ReadReply readReply) {
……
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
……
}
});
}
您查询步数的那几行没有问题。问题在于授权授予。需要注意的是,应用可以访问的数据必须在华为开发者网站上授权给应用的范围内,并且在设备端已经授权用户授权的数据范围内。
针对该问题,请确认在应用显示数据授权界面时,已授予步数数据的read/write权限。
您可以参考以下代码,或参考Health Kit的示例代码
https://developer.huawei.com/consumer/en/doc/development/HMS-Examples/healthkit_Android_sample_code.
private void dataAuthorization() {
Log.i(TAG, "begin sign in");
// The data that can be used here and its read/write permissions can only be those you have applied for from the Huawei Developers website.
List < Scope > scopeList = new ArrayList < > ();
scopeList.add(new Scope(Scopes.HEALTHKIT_STEP_BOTH));
HuaweiIdAuthParamsHelper authParamsHelper =
new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM);
HuaweiIdAuthParams authParams =
authParamsHelper.setIdToken().setAccessToken().setScopeList(scopeList).createParams();
final HuaweiIdAuthService authService =
HuaweiIdAuthManager.getService(this.getApplicationContext(), authParams);
Task < AuthHuaweiId > authHuaweiIdTask = authService.silentSignIn();
authHuaweiIdTask.addOnSuccessListener(huaweiId - > {
......
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
......
}
});
}
使用DataController
下对应方法查询步数返回错误码50005
怎么办? (我在华为开发者网站申请的Health Kit scope已经通过)
2020-05-26 11:41:21.195 17338-17338/com.hauwei.hmsdemo I/DataManager: read failure 50005:Unknown authorization error
2020-05-26 11:41:21.203 17338-17338/com.hauwei.hmsdemo I/DataManager:
以下几行用于查询步数:
public void readSteps(View view) throws ParseException {
DataCollector dataCollector = new DataCollector.Builder().setPackageName(context)
.setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
.setDataStreamName("STEPS_DELTA")
.setDataGenerateType(DataCollector.DATA_TYPE_RAW)
.build();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date startDate = dateFormat.parse(BEGIN_TIME);
Date endDate = dateFormat.parse(END_TIME);
ReadOptions readOptions = new ReadOptions.Builder().read(dataCollector)
.setTimeRange(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS)
.build();
dataController.read(readOptions).addOnSuccessListener(new OnSuccessListener<ReadReply>() {
@Override
public void onSuccess(ReadReply readReply) {
……
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
……
}
});
}
您查询步数的那几行没有问题。问题在于授权授予。需要注意的是,应用可以访问的数据必须在华为开发者网站上授权给应用的范围内,并且在设备端已经授权用户授权的数据范围内。
针对该问题,请确认在应用显示数据授权界面时,已授予步数数据的read/write权限。 您可以参考以下代码,或参考Health Kit的示例代码 https://developer.huawei.com/consumer/en/doc/development/HMS-Examples/healthkit_Android_sample_code.
private void dataAuthorization() {
Log.i(TAG, "begin sign in");
// The data that can be used here and its read/write permissions can only be those you have applied for from the Huawei Developers website.
List < Scope > scopeList = new ArrayList < > ();
scopeList.add(new Scope(Scopes.HEALTHKIT_STEP_BOTH));
HuaweiIdAuthParamsHelper authParamsHelper =
new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM);
HuaweiIdAuthParams authParams =
authParamsHelper.setIdToken().setAccessToken().setScopeList(scopeList).createParams();
final HuaweiIdAuthService authService =
HuaweiIdAuthManager.getService(this.getApplicationContext(), authParams);
Task < AuthHuaweiId > authHuaweiIdTask = authService.silentSignIn();
authHuaweiIdTask.addOnSuccessListener(huaweiId - > {
......
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
......
}
});
}