GithubHelp home page GithubHelp logo

aekanman / sleep-history Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 165 KB

:sleeping:Android app to read/write sleep data on Google Fitness API

License: Apache License 2.0

Java 100.00%
android-wear google-fit-api

sleep-history's People

Contributors

aekanman avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

sleep-history's Issues

Sleep Data

Hello Aekanman,

Below is my function to get sleep data but its not provide sleep even i have added in google fit.

Thanks in advance

private void readSleepSessions() {


    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);

    long endTime = cal.getTimeInMillis();

// cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.add(Calendar.DAY_OF_MONTH, -1);

    long startTime = cal.getTimeInMillis();


    DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(requireContext());

    Log.i(TAG, "SLEEP Range Start: " + dateFormat.format(startTime));
    Log.i(TAG, "SLEEP Range End: " + dateFormat.format(endTime));


    SessionReadRequest request = new SessionReadRequest.Builder()
            .readSessionsFromAllApps()
            .includeSleepSessions()
            // Sleep segment data is required for details of the fine-granularity sleep, if it is present.
            .read(DataType.TYPE_SLEEP_SEGMENT)
            .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();


    Fitness.getSessionsClient(requireContext(),getGoogleSigningAccount())
            .readSession(request)
            .addOnSuccessListener(new OnSuccessListener<SessionReadResponse>() {
                @Override
                public void onSuccess(SessionReadResponse sessionReadResponse) {

                    for (int i = 0; i < sessionReadResponse.getSessions().size(); i++) {

                        Session session = sessionReadResponse.getSessions().get(i);
                        // If the sleep session has finer granularity sub-components, extract them:
                        List<DataSet> dataSets = sessionReadResponse.getDataSet(session);

                        for (int j = 0; j < dataSets.size(); j++) {
                            DataSet mDataSet = dataSets.get(j);
                            for (DataPoint point : mDataSet.getDataPoints()) {
                                String sleepStageVal = "" + point.getValue(Field.FIELD_SLEEP_SEGMENT_TYPE).asInt();
                                long segmentStart = point.getStartTime(TimeUnit.MILLISECONDS);
                                long segmentEnd = point.getEndTime(TimeUnit.MILLISECONDS);

                                Log.i(TAG, "\t* Type $sleepStage between $segmentStart and $segmentEnd" + segmentStart + "" + segmentEnd);
                            }
                        }

                    }

                }
            });

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.