Skip to content

Commit

Permalink
Fixed error that did not show heart rate.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jan 27, 2020
1 parent eb8d2b7 commit 15b6c8b
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ out/
# Gradle files
.gradle/
build/
app/release/

# Local configuration file (sdk path, etc)
local.properties
Expand Down
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

[![Download][apk-download]][apk-download-url]

<p align="center"><img src="https://i.imgur.com/Z31yxK2.png"/></p>
<p align="center"><img width="120" src="https://i.imgur.com/Z31yxK2.png"/></p>

Native Android application responsible for OCARIoT platform data acquisition. The application acts as an external services access token manager. Currently, only Fitbit service is available.

**Main Features:**
- Fitbit access token management:
- **Family**, **Educator** or **Healthcare Professional** may grant the OCARIoT platform access to collect Fitbit data from children who have privileges to manage their data;
- Fitbit access revocation;
- Support for Fitbit data synchronization with OCARIoT platform.
- Listing of data saved on OCARIoT platform:
- Physical Activity;
- The educator and the qualified professional can grant the OCARIoT platform permission to collect Fitbit data from children who have privileges to manage their data according to the consent of those responsible;
- Support to request collection of Fitbit data from the child at any time, automatically saving to the OCARIoT platform;
- Revocation of permission to collect Fitbit data;

- Display of data saved on the OCARIoT platform:
- Physical activity;
- Sleep;
- Weight.
- Display of Heart Rate data collected in real time from Polar OH1 device.

- Display of heart rate data collected in real time. Supported devices:
- Polar OH;
- Polar H10.


## Prerequisites
- Android SDK v28
Expand All @@ -28,7 +33,6 @@ Native Android application responsible for OCARIoT platform data acquisition. Th

```console
git clone https://github.com/ocariot/da-app.git

```
2. **Set up the environment:**
- Make a copy of the `gradle.properties.example` file in the `/app` directory named `gradle.properties` _(This file will not be tracked by git because it is in .gitignore)_.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package br.edu.uepb.nutes.ocariot.data.repository.remote;

import android.annotation.SuppressLint;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -14,6 +16,8 @@

import br.edu.uepb.nutes.ocariot.BuildConfig;
import br.edu.uepb.nutes.ocariot.OcariotApp;
import br.edu.uepb.nutes.ocariot.exception.ConnectivityException;
import br.edu.uepb.nutes.ocariot.utils.ConnectionUtils;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand All @@ -39,8 +43,11 @@ public BaseNetRepository() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(s -> Timber.tag("OkHttp").d(s));
logging.level(HttpLoggingInterceptor.Level.BODY);
this.addInterceptor(logging);
this.addInterceptor(new NetworkConnectionInterceptor());
}
this.addInterceptor(chain -> {
if (!ConnectionUtils.isNetworkAvailable(OcariotApp.getContext())) throw new ConnectivityException();
return chain.proceed(chain.request().newBuilder().build());
});
}

protected Retrofit provideRetrofit(String baseUrl) {
Expand Down Expand Up @@ -79,11 +86,13 @@ private OkHttpClient.Builder getUnsafeOkHttpClient() {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
// Not implemented!
}

@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
// Not implemented!
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static boolean isSupportedBluetooth() {
*
* @return true to enabled or false otherwise.
*/
public static boolean bluetoothIsEnabled() {
public static boolean isBluetoothAvailable() {
return BluetoothAdapter.getDefaultAdapter() != null &&
BluetoothAdapter.getDefaultAdapter().isEnabled();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public static Date convertDateTime(String datetime) {
public static String convertDateTimeToUTC(String datetime) {
if (datetime == null) return null;

Date dateUTC = null;
Date dateUTC;
DateFormat formatUTC = new SimpleDateFormat(DateUtils.DATE_FORMAT_DATE_TIME, Locale.getDefault());

try {
dateUTC = formatUTC.parse(datetime);
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
if (datetime.contains("Z")) formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
} catch (ParseException e) {
return "";
}
Expand All @@ -103,13 +103,13 @@ public static String convertDateTimeUTCToLocale(String datetime, String formatDa
if (timezone == null) timezone = TimeZone.getDefault();
if (formatDate == null) formatDate = DateUtils.DATE_FORMAT_DATE_TIME;

Date dateUTC = null;
Date dateUTC;
DateFormat formatLocale = new SimpleDateFormat(formatDate, Locale.getDefault());
formatLocale.setTimeZone(timezone);

try {
DateFormat utcFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT_DATE_TIME, Locale.getDefault());
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
if (datetime.contains("Z")) utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dateUTC = utcFormat.parse(datetime);
} catch (ParseException e) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public void showData(RecyclerView.ViewHolder holder, int position, List<Sleep> i
final Sleep sleep = itemsList.get(position);
ViewHolderSleep h = (ViewHolderSleep) holder;

h.dateStart.setText(DateUtils.convertDateTimeUTCToLocale(sleep.getStartTime(),
h.dateStart.setText(DateUtils.convertDateTimeUTCToLocale(sleep.getEndTime(),
context.getResources().getString(R.string.date_time_abb4), null));
h.period.setText(String.format(Locale.getDefault(), "%s - %s",
DateUtils.convertDateTimeUTCToLocale(sleep.getStartTime(),
context.getResources().getString(R.string.hour_format2), null),
context.getResources().getString(R.string.hour_format1), null),
DateUtils.convertDateTimeUTCToLocale(sleep.getEndTime(),
context.getResources().getString(R.string.hour_format2), null)));
context.getResources().getString(R.string.hour_format1), null)));
h.duration.setText(String.format(Locale.getDefault(), "%02dhrs %02dmin",
sleep.getDuration() / 3600000, (sleep.getDuration() / 60000) % 60));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void initToolbar() {
mActionBar.setHomeAsUpIndicator(R.drawable.ic_close_dark);

if (sleep.getStartTime() != null) {
mActionBar.setTitle(DateUtils.convertDateTimeUTCToLocale(sleep.getStartTime(),
mActionBar.setTitle(DateUtils.convertDateTimeUTCToLocale(sleep.getEndTime(),
getString(R.string.date_time_abb4), null));
}
mActionBar.setSubtitle(appPref.getLastSelectedChild().getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void onStart() {
public void onResume() {
super.onResume();
loadDataOcariot();
if (ConnectionUtils.bluetoothIsEnabled()) {
if (ConnectionUtils.isBluetoothAvailable()) {
mBoxEnableBluetooth.setVisibility(View.GONE);
if (!hasLocationPermissions()) {
requestLocationPermission();
Expand All @@ -213,7 +213,7 @@ public void onResume() {
public void onStop() {
super.onStop();
mContext.unregisterReceiver(mBluetoothReceiver);
if (ConnectionUtils.bluetoothIsEnabled()) mScanner.stopScan();
if (ConnectionUtils.isBluetoothAvailable()) mScanner.stopScan();
}

@Override
Expand Down Expand Up @@ -358,21 +358,27 @@ private void updateViewHR(int hr, String timestamp) {
Objects.requireNonNull(getActivity())
.runOnUiThread(() -> {
// Update chart
addEntry((float) hr);

if (heartAnimation.isPaused() || !heartAnimation.isRunning()) {
heartAnimation.start();
ImageViewCompat.setImageTintList(mHeartImage, ColorStateList.valueOf(
getResources().getColor(R.color.colorDanger)));
mBoxHR.setVisibility(View.VISIBLE);
mBoxHRSummary.setVisibility(View.VISIBLE);
try {
addEntry((float) hr);

if (heartAnimation.isPaused() || !heartAnimation.isRunning()) {
heartAnimation.start();
ImageViewCompat.setImageTintList(mHeartImage, ColorStateList.valueOf(
getResources().getColor(R.color.colorDanger)));
mBoxHR.setVisibility(View.VISIBLE);
mBoxHRSummary.setVisibility(View.VISIBLE);
}
mHR.setText(String.valueOf(hr));

// Update summary
if (totalHR > 0) {
mMinHRTextView.setText(String.valueOf(minHR));
mMaxHRTextView.setText(String.valueOf(maxHR));
mAvgHRTextView.setText(String.valueOf(sumHR / totalHR));
}
} catch (RuntimeException e) {
Timber.d("Error adding chart entry: %s", e.getMessage());
}
mHR.setText(String.valueOf(hr));

// Update summary
mMinHRTextView.setText(String.valueOf(minHR));
mMaxHRTextView.setText(String.valueOf(maxHR));
mAvgHRTextView.setText(String.valueOf(sumHR / totalHR));
});
}

Expand All @@ -399,6 +405,7 @@ private void initChart() {

// enable touch gestures
mChart.setTouchEnabled(true);
mChart.animateX(1500);

// enable scaling and dragging
mChart.setDragEnabled(true);
Expand Down Expand Up @@ -513,7 +520,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
requestLocationPermission();
return;
}
if (ConnectionUtils.bluetoothIsEnabled()) {
if (ConnectionUtils.isBluetoothAvailable()) {
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
}
Expand Down Expand Up @@ -559,6 +566,7 @@ public void onMeasurementReceived(@NonNull BluetoothDevice device, int heartRate
@Override
public void onConnected(@NonNull BluetoothDevice device) {
Timber.d("onConnected(): %s", device.getName());
device.createBond();
}

@Override
Expand All @@ -568,5 +576,9 @@ public void onDisconnected(@NonNull BluetoothDevice device) {
ImageViewCompat.setImageTintList(mHeartImage, ColorStateList.valueOf(
getResources().getColor(R.color.colorLineDivider)));
mBoxHR.setVisibility(View.GONE);
if (ConnectionUtils.isBluetoothAvailable()) {
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void loadDataOcariot() {
ocariotRepository
.listActivities(
appPref.getLastSelectedChild().getId(),
"-start_time",
"-end_time",
1,
100
)
Expand Down

0 comments on commit 15b6c8b

Please sign in to comment.