无需Google登录即可从传感器读取用户的步数和其他健身数据

时间:2018-12-28 12:52:50

标签: android android-sensors google-api-client google-fit google-fit-sdk

我正在开发一个应用程序,该应用程序要从两个移动传感器以及Google Fit支持的其他健身设备读取用户的步数以及其他与健身有关的信息,例如心率(连接到智能手环时的心率) SDK。

我正在尝试使用Google的Fit SDK for android,并尝试通过Sensor API访问数据,但是每次执行代码时,我都会收到错误消息:“需要登录”。 我不希望用户登录到特定的Google帐户。我有自己的登录系统。 我也不想将数据存储在健身商店中,因为我有自己的后端。我要做的就是从传感器访问数据。

这是我尝试的伪代码:

private GoogleApiClient googleApiClient;

private static final int REQUEST_OAUTH = 1001;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);
                return true;
        }
        return false;
    }
};

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

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    googleApiClient.connect();

    PendingResult<Status> pendingResult = Fitness.SensorsApi.add(
            googleApiClient,
            new SensorRequest.Builder()
                    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
                    .setSamplingRate(1, TimeUnit.MINUTES)  // sample once per minute
                    .build(),
            new OnDataPointListener() {
                @Override
                public void onDataPoint(DataPoint dataPoint) {
                    Log.e("Data Point", dataPoint.toString());
                }
            });
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == REQUEST_OAUTH && resultCode == RESULT_OK) {
        googleApiClient.connect();
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.getErrorCode() == FitnessStatusCodes.NEEDS_OAUTH_PERMISSIONS) {
        try {
            connectionResult.startResolutionForResult(this, REQUEST_OAUTH);
        } catch (IntentSender.SendIntentException e) {
        }
    }
}

是否可以通过fitness sdk来实现此目的而无需登录,如果没有登录,还有什么其他方法可以在Android中访问此信息。

我在某处阅读了可以使用传感器管理器的信息,但我想我必须自己进行所有计算,例如删除由于突然的急动等原因而从传感器接收到的不良数据点,我认为fitness-sdk已经在这样做。

0 个答案:

没有答案