Skip to content

Commit

Permalink
Fixed error in bluetooth pairing and asking for location permission.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Feb 4, 2020
1 parent b702053 commit f981051
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -139,6 +141,12 @@ public class IotFragment extends Fragment implements View.OnClickListener, HRMan
@BindView(R.id.alert_enable_bluetooth)
FrameLayout mBoxEnableBluetooth;

@BindView(R.id.alert_enable_location)
FrameLayout mBoxEnableLocation;

@BindView(R.id.box_hr_progress_bar)
LinearLayout mBoxHRLoading;

/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
Expand Down Expand Up @@ -174,7 +182,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_iot, container, false);
ButterKnife.bind(this, view);

return view;
}

Expand All @@ -200,10 +207,15 @@ public void onResume() {

if (ConnectionUtils.isBluetoothAvailable()) {
mBoxEnableBluetooth.setVisibility(View.GONE);

if (!hasLocationPermissions()) {
requestLocationPermission();
return;
} else {
mBoxEnableLocation.setVisibility(View.GONE);
}
if (mHRManager.isConnected()) mBoxHRLoading.setVisibility(View.GONE);
else mBoxHRLoading.setVisibility(View.VISIBLE);
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
}
Expand All @@ -230,6 +242,7 @@ private void initComponents() {
initChart();
initAnimation();
mBoxEnableBluetooth.setOnClickListener(this);
mBoxEnableLocation.setOnClickListener(this);
}

/**
Expand Down Expand Up @@ -484,6 +497,15 @@ public void onClick(View v) {
if (v.getId() == R.id.alert_enable_bluetooth) {
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
REQUEST_ENABLE_BLUETOOTH);
} else if (v.getId() == R.id.alert_enable_location) {
startActivity(
new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts(
"package",
Objects.requireNonNull(getActivity()).getPackageName(),
null
))
);
}
}

Expand All @@ -505,6 +527,12 @@ private boolean hasLocationPermissions() {
* Request Location permission.
*/
private void requestLocationPermission() {
if (!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
if (mBoxEnableBluetooth.getVisibility() == View.GONE) {
mBoxEnableLocation.setVisibility(View.VISIBLE);
}
return;
}
ActivityCompat.requestPermissions(Objects.requireNonNull(getActivity()),
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ENABLE_LOCATION);
}
Expand All @@ -517,10 +545,10 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
if ((requestCode == REQUEST_ENABLE_LOCATION) &&
(grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(mContext, R.string.message_permission_location, Toast.LENGTH_LONG).show();
requestLocationPermission();
return;
}
if (ConnectionUtils.isBluetoothAvailable()) {
mBoxEnableLocation.setVisibility(View.GONE);
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
}
Expand All @@ -537,10 +565,12 @@ public void onReceive(Context context, Intent intent) {
switch (state) {
case BluetoothAdapter.STATE_OFF:
mBoxEnableBluetooth.setVisibility(View.VISIBLE);
mBoxHRLoading.setVisibility(View.GONE);
break;
case BluetoothAdapter.STATE_ON:
Timber.d("BluetoothAdapter.STATE_ON");
mBoxEnableBluetooth.setVisibility(View.GONE);
mBoxHRLoading.setVisibility(View.VISIBLE);
if (!hasLocationPermissions()) requestLocationPermission();
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
Expand All @@ -560,13 +590,13 @@ public void onMeasurementReceived(@NonNull BluetoothDevice device, int heartRate
minHR = heartRate < minHR ? heartRate : minHR;
maxHR = heartRate > maxHR ? heartRate : maxHR;
}
mBoxHRLoading.setVisibility(View.GONE);
updateViewHR(heartRate, timestamp);
}

@Override
public void onConnected(@NonNull BluetoothDevice device) {
Timber.d("onConnected(): %s", device.getName());
device.createBond();
}

@Override
Expand All @@ -577,6 +607,7 @@ public void onDisconnected(@NonNull BluetoothDevice device) {
getResources().getColor(R.color.colorLineDivider)));
mBoxHR.setVisibility(View.GONE);
if (ConnectionUtils.isBluetoothAvailable()) {
mBoxHRLoading.setVisibility(View.VISIBLE);
mScanner.stopScan();
mScanner.startScan(mScannerCallback);
}
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/res/layout/fragment_iot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@
android:textColor="@android:color/white" />
</FrameLayout>

<FrameLayout
android:id="@+id/alert_enable_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWarning"
android:orientation="vertical"
android:padding="@dimen/default_padding"
android:visibility="visible">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center"
android:text="@string/alert_enable_location"
android:textColor="@android:color/white" />
</FrameLayout>

<include
android:id="@+id/box_no_data"
layout="@layout/no_data"
Expand Down Expand Up @@ -154,6 +172,21 @@
android:src="@drawable/heart"
android:tint="@color/colorLineDivider" />

<LinearLayout
android:id="@+id/box_hr_progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">

<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginTop="5dp"/>
</LinearLayout>

<LinearLayout
android:id="@+id/box_hr"
android:layout_width="match_parent"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
<string name="alert_no_token_fitbit">Για να συλλέξετε δεδομένα Fitbit από τα παιδια \"%s\", πρέπει πρώτα να δώσετε εξουσιοδότηση για τη συλλογή των δεδομένων.</string>
<string name="alert_access_blocked">Λυπούμαστε, αυτή η εφαρμογή δεν υποστηρίζει αυτον τον τύπο χρηστη. Επιτρέπονται μόνο οικογένειες, εκπαιδευτικοί και επαγγελματίες υγείας&#8230;</string>
<string name="alert_revoke_fitbit_success">Η πρόσβαση Fitbit ακυρώθηκε με επιτυχία. Δεν θα είστε πλέον σε θέση να πραγματοποιήσετε συγχρονισμό δεδομένων.</string>
<string name="alert_enable_bluetooth">Αν θέλετε να εμφανίζονται αυτόματα τα δεδομένα της καρδιακής συχνότητας (Polar OH1) σε αυτήν την οθόνη, κάντε κλικ εδώ για να ενεργοποιήσετε το Bluetooth&#8230;</string>
<string name="alert_enable_bluetooth">Αν θέλετε να εμφανίζονται αυτόματα τα δεδομένα της καρδιακής συχνότητας (Polar OH1, H10&#8230;) σε αυτήν την οθόνη, κάντε κλικ εδώ για να ενεργοποιήσετε το Bluetooth&#8230;</string>
<string name="alert_enable_location">If you want your Heart Rate (Polar OH1, H10&#8230;) data to be displayed automatically on this screen, you must grant access to your location. Click here to grant&#8230;</string>
<string name="alert_back_confirm">Πατήστε ξανά για έξοδο!</string>

<!-- MENU -->
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
<string name="alert_no_token_fitbit">Para recopilar datos de Fitbit del niño \"%s\", primero debe proporcionar autorización.</string>
<string name="alert_access_blocked">Lo sentimos, esta aplicación sólo permite el acceso a la familia, educador o profesional de la salud&#8230;</string>
<string name="alert_revoke_fitbit_success">Acceso a Fitbit revocado correctamente. Ya no podrá realizar la sincronización de datos.</string>
<string name="alert_enable_bluetooth">Si desea que sus datos de frecuencia cardíaca (Polar OH1) se muestren automáticamente en esta pantalla, haga clic aquí para habilitar Bluetooth&#8230;</string>
<string name="alert_enable_bluetooth">Si desea que sus datos de frecuencia cardíaca (Polar OH1, H10&#8230;) se muestren automáticamente en esta pantalla, haga clic aquí para habilitar Bluetooth&#8230;</string>
<string name="alert_enable_location">Si desea que sus datos de frecuencia cardíaca (Polar OH1, H10&#8230;) se muestren automáticamente en esta pantalla, debe otorgar acceso a su ubicación. Haga clic aquí para otorgar&#8230;</string>
<string name="alert_back_confirm">Toque de nuevo para salir!</string>

<!-- MENU -->
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
<string name="alert_no_token_fitbit">Para coletar dados Fitbit da criança \"%s\", você deve primeiro fornecer autorização para coletar os dados.</string>
<string name="alert_access_blocked">Desculpe, este aplicativo permite apenas acesso a família, educador ou profissional de saúde&#8230;</string>
<string name="alert_revoke_fitbit_success">Acesso Fitbit revogado com sucesso. Não será mais possível executar a sincronização dos dados.</string>
<string name="alert_enable_bluetooth">Se você deseja que os dados de Frequência Cardíaca (Polar OH1) sejam exibidos automaticamente nesta tela, clique aqui para ativar o Bluetooth&#8230;</string>
<string name="alert_enable_bluetooth">Se você deseja que os dados de Frequência Cardíaca (Polar OH1, H10&#8230;) sejam exibidos automaticamente nesta tela, clique aqui para ativar o Bluetooth&#8230;</string>
<string name="alert_enable_location">Se você deseja que os dados de Frequência Cardíaca (Polar OH1, H10&#8230;) sejam exibidos automaticamente nesta tela, é necessário conceder acesso a sua localização. Clique aqui para conceder&#8230;</string>
<string name="alert_back_confirm">Toque novamente para sair!</string>

<!-- MENU -->
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
<string name="alert_no_token_fitbit">To collect Fitbit data from child \"%s\", you must first provide authorization to collect the data.</string>
<string name="alert_access_blocked">Sorry, this app only allows access to family, educator or health professional.&#8230;</string>
<string name="alert_revoke_fitbit_success">Fitbit access successfully revoked. You will no longer be able to perform data synchronization.</string>
<string name="alert_enable_bluetooth">If you want your Heart Rate (Polar OH1) data to be automatically displayed on this screen, click here to enable Bluetooth&#8230;</string>
<string name="alert_enable_bluetooth">If you want your Heart Rate (Polar OH1, H10&#8230;) data to be automatically displayed on this screen, click here to enable Bluetooth&#8230;</string>
<string name="alert_enable_location">If you want your Heart Rate (Polar OH1, H10&#8230;) data to be displayed automatically on this screen, you must grant access to your location. Click here to grant&#8230;</string>
<string name="alert_back_confirm">Tap again to exit!</string>

<!-- MENU -->
Expand Down

0 comments on commit f981051

Please sign in to comment.