Created
September 21, 2014 21:57
-
-
Save raveeshbhalla/4d798cb1d7ac2943ea8a to your computer and use it in GitHub Desktop.
Google Fit API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
09-22 03:18:34.416 27708-27708/app.fitrank I/MainActivity﹕ Connecting... | |
09-22 03:18:34.447 27708-27708/app.fitrank I/am_on_resume_called﹕ [0,app.fitrank.MainActivity] | |
09-22 03:18:34.453 27708-27739/app.fitrank D/CanvasContext﹕ Render dirty regions requested: true | |
09-22 03:18:34.491 27708-27739/app.fitrank W/Adreno-GSL﹕ <ioctl_kgsl_device_getproperty:663>: mmap failed: errno 22 Invalid argument | |
09-22 03:18:34.491 27708-27739/app.fitrank I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I10246dbd022c719c705be805d5642cc8fdfbd2a2Date: 03/07/14 | |
09-22 03:18:34.492 27708-27739/app.fitrank I/CanvasContext﹕ Initialized EGL, version 1.4 | |
09-22 03:18:34.510 27708-27739/app.fitrank D/OpenGLRenderer﹕ Enabling debug mode 0 | |
09-22 03:18:34.522 27708-27708/app.fitrank I/MainActivity﹕ Connected! | |
09-22 03:18:34.562 27708-27727/app.fitrank V/Fitness﹕ Received batch result | |
09-22 03:18:34.581 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.583 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.584 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.584 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.584 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.585 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta | |
09-22 03:18:34.585 27708-27708/app.fitrank I/MainActivity﹕ Data type: com.google.step_count.delta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.fitrank; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.IntentSender; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; | |
import com.google.android.gms.common.api.PendingResult; | |
import com.google.android.gms.common.api.ResultCallback; | |
import com.google.android.gms.fitness.*; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
public class MainActivity extends Activity | |
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
private static final String TAG = "MainActivity"; | |
private static final int REQUEST_OAUTH = 9000; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
private GoogleApiClient mClient = null; | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
if (mClient == null || !mClient.isConnected()) { | |
connectFitness(); | |
} | |
} | |
private void connectFitness() { | |
Log.i(TAG, "Connecting..."); | |
// Create the Google API Client | |
mClient = new GoogleApiClient.Builder(this) | |
// select the Fitness API | |
.addApi(Fitness.API) | |
// specify the scopes of access | |
.addScope(FitnessScopes.SCOPE_ACTIVITY_READ) | |
//Don't need the next, here just for learning | |
//.addScope(FitnessScopes.SCOPE_BODY_READ_WRITE) | |
// provide callbacks | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
// Connect the Google API client | |
mClient.connect(); | |
} | |
// Manage OAuth authentication | |
@Override | |
public void onConnectionFailed(ConnectionResult result) { | |
// Error while connecting. Try to resolve using the pending intent returned. | |
if (result.getErrorCode() == ConnectionResult.SIGN_IN_REQUIRED || | |
result.getErrorCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS) { | |
try { | |
// Request authentication | |
result.startResolutionForResult(this, REQUEST_OAUTH); | |
} catch (IntentSender.SendIntentException e) { | |
Log.e(TAG, "Exception connecting to the fitness service", e); | |
} | |
} else { | |
Log.e(TAG, "Unknown connection issue. Code = " + result.getErrorCode()); | |
} | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_OAUTH) { | |
if (resultCode == RESULT_OK) { | |
// If the user authenticated, try to connect again | |
mClient.connect(); | |
} | |
} | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
// If your connection gets lost at some point, | |
// you'll be able to determine the reason and react to it here. | |
if (i == ConnectionCallbacks.CAUSE_NETWORK_LOST) { | |
Log.i(TAG, "Connection lost. Cause: Network Lost."); | |
} else if (i == ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { | |
Log.i(TAG, "Connection lost. Reason: Service Disconnected"); | |
} | |
} | |
@Override | |
public void onConnected(Bundle bundle) { | |
Log.i(TAG, "Connected!"); | |
// Now you can make calls to the Fitness APIs. | |
invokeFitnessAPIs(); | |
} | |
public void invokeFitnessAPIs() { | |
// Call the Fitness APIs here | |
// Call the Fitness APIs here | |
// 1. Obtain start and end times | |
// (In this example, the start time is one week before this moment) | |
long WEEK_IN_MS = 1000 * 60 * 60 * 24 * 7; | |
Date now = new Date(); | |
long endTime = now.getTime(); | |
long startTime = endTime - (WEEK_IN_MS); | |
// 2. Create a data request specifying data types and a time range | |
// (In this example, group the data to find how many steps were walked per day) | |
DataReadRequest readreq = new DataReadRequest.Builder() | |
.addAggregatedDefaultDataSource(DataTypes.STEP_COUNT_DELTA) | |
.bucketByTime(1, TimeUnit.DAYS) | |
.setTimeRange(startTime, endTime) | |
.build(); | |
// 3. Invoke the History API with: | |
// - The Google API client object | |
// - The read data request | |
PendingResult<DataReadResult> pendingResult = | |
Fitness.HistoryApi.readData(mClient, readreq); | |
// 4. Access the results of the query asynchronously | |
// (The result is not immediately available) | |
pendingResult.setResultCallback( | |
new ResultCallback<DataReadResult>() { | |
@Override | |
public void onResult(DataReadResult readDataResult) { | |
// If the request specified aggregated data, the data is returned as buckets | |
// that contain lists of DataSet objects | |
if (readDataResult.getBuckets().size() > 0) { | |
for (Bucket bucket : readDataResult.getBuckets()) { | |
List<DataSet> dataSets = bucket.getDataSets(); | |
for (DataSet dataSet : dataSets) { | |
// Show the data points (see next example) | |
dumpDataSet(dataSet); | |
} | |
} | |
// Otherwise, the data is returned as a list of DataSet objects | |
} else if (readDataResult.getDataSets().size() > 0) { | |
for (DataSet dataSet : readDataResult.getDataSets()) { | |
// Show the data points (see next example) | |
dumpDataSet(dataSet); | |
} | |
} | |
} | |
} | |
); | |
} | |
public void dumpDataSet(DataSet dataSet) { | |
Log.i(TAG, "Data type: " + dataSet.getDataType().getName()); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); | |
for (DataPoint dp : dataSet.getDataPoints()) { | |
// Obtain human-readable start and end times | |
long dpStart = dp.getStartTimeNanos() / 1000000; | |
long dpEnd = dp.getEndTimeNanos() / 1000000; | |
Log.i(TAG, "Data point:"); | |
Log.i(TAG, "\tType: " + dp.getDataType().getName()); | |
Log.i(TAG, "\tStart: " + dateFormat.format(dpStart)); | |
Log.i(TAG, "\tEnd: " + dateFormat.format(dpEnd)); | |
for(DataType.Field field : dp.getDataType().getFields()) { | |
String fieldName = field.getName(); | |
Log.i(TAG, "\tField: " + fieldName + | |
" Value: " + dp.getValue(field)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello.
Fitness.API, FitnessScopes.SCOPE_ACTIVITY_READ
I can't use
in the build grade
Implementation 'com.Google.android.gms:play-services:6.1.+'
I can't bring it.
What kind of service do I need?
regards dolyun...