gpt4 book ai didi

google-fit - Fit Api 中的距离

转载 作者:行者123 更新时间:2023-12-01 09:31:02 26 4
gpt4 key购买 nike

我在使用 Google Fit Api 获取行进距离时遇到问题。我对计步器使用了类似的方法并且有效。它只是说听众已注册。

大部分代码来自 Github 示例。

有什么问题吗?

public class MainActivity extends Activity {

public static final String TAG = "BasicSensorsApi";
private static final int REQUEST_OAUTH = 1;
private OnDataPointListener mListener;
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;

TextView dispSteps;
long numSteps;

private GoogleApiClient mClient = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}

dispSteps=(TextView)findViewById(R.id.dispStepsTV);
numSteps=0;

buildFitnessClient();

}

private void updateDispSteps(final int update) {

numSteps+=update;

runOnUiThread(new Runnable() {
@Override
public void run() {
dispSteps.setText( String.valueOf(numSteps));
}
});
}

private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {

@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");

Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_DISTANCE_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");

} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing.");

}
}
});

mListener = new OnDataPointListener() {
@Override
public void onDataPoint(DataPoint dataPoint) {
for (Field field : dataPoint.getDataType().getFields()) {
Value value = dataPoint.getValue(field);
updateDispSteps(value.asInt());
Log.i("BIG BLUE TEXT", value.toString());
}
}
};

SensorRequest req = new SensorRequest.Builder()
.setDataType(DataType.TYPE_DISTANCE_DELTA)
.setSamplingRate(10, TimeUnit.SECONDS)
.build();

Fitness.SensorsApi.add(mClient, req, mListener)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Listener registered!");
} else {
Log.i(TAG, "Listener not registered.");
}
}
});

}

@Override
public void onConnectionSuspended(int i) {
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
MainActivity.this, 0).show();
return;
}
if (!authInProgress) {
try {
Log.i(TAG, "Attempting to resolve failed connection");
authInProgress = true;
result.startResolutionForResult(MainActivity.this,
REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Exception while starting resolution activity", e);
}
}
}
}
)
.build();
}

PS:这是我在 stackoverflow 上的第一个问题 :P

最佳答案

I think sensor api is used to calculate distance and other values, recoding is for storing them.I used the sensor api and it works for me.

private void buildFitnessClient() {
if (mClient == null && checkPermissions()) {
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))....}}


private void findDistDataSrc() {
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE,
DataType.TYPE_DISTANCE_DELTA)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_RAW, DataSource.TYPE_DERIVED)
.build())
.setResultCallback(new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(DataSourcesResult dataSourcesResult) {
//Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString());
for (DataSource dataSource : dataSourcesResult.getDataSources()) {
//Log.i(TAG, "Data source found: " + dataSource.toString());
//Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName());
final DataType dataType = dataSource.getDataType();
if ((dataType.equals(DataType.TYPE_STEP_COUNT_CUMULATIVE) || dataType.equals(DataType.TYPE_DISTANCE_DELTA)) && mListener == null) {
Log.i(TAG, "Data source for " + dataType.toString() + " found! Registering.");
..........}

关于google-fit - Fit Api 中的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com