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