Skip to content

Commit

Permalink
check for Bluetooth connect and scan permissions for connecting >= An…
Browse files Browse the repository at this point in the history
…droid 12
  • Loading branch information
oliexdev committed Nov 15, 2021
1 parent 9947c5b commit be98e37
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.health.openscale.gui;

import android.Manifest;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
Expand All @@ -24,6 +25,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.net.Uri;
Expand Down Expand Up @@ -75,6 +77,7 @@
import com.health.openscale.gui.preferences.BluetoothSettingsFragment;
import com.health.openscale.gui.preferences.UserSettingsFragment;
import com.health.openscale.gui.slides.AppIntroActivity;
import com.health.openscale.gui.utils.PermissionHelper;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -582,6 +585,12 @@ private void invokeConnectToBluetoothDevice() {
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S) {
Timber.d("SDK >= 31 request for Bluetooth Scan and Bluetooth connect permissions");
requestPermissions(new String[]{Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT}, PermissionHelper.PERMISSIONS_REQUEST_ACCESS_BLUETOOTH);
return;
}

Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show();
setBluetoothStatusIcon(R.drawable.ic_bluetooth_searching);

Expand Down Expand Up @@ -877,6 +886,32 @@ private void shareCsvFile() {
startActivity(Intent.createChooser(intent, getResources().getString(R.string.label_share)));
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_BLUETOOTH: {
boolean allGranted = true;
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
}
}

if (allGranted) {
Timber.d("All Bluetooth permissions granted");
invokeConnectToBluetoothDevice();
} else {
Timber.d("At least one Bluetooth permission was not granted");
Toast.makeText(this, R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
}
break;
}
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,24 @@ public static boolean requestBluetoothPermission(final Fragment fragment) {
}

private static boolean requestLocationPermission(final Fragment fragment, String[] requiredPermissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (requestLocationServicePermission(fragment)) {
if (fragment.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getActivity());

builder.setMessage(R.string.permission_bluetooth_info)
.setTitle(R.string.permission_bluetooth_info_title)
.setIcon(R.drawable.ic_preferences_about)
.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
fragment.requestPermissions(requiredPermissions, PERMISSIONS_REQUEST_ACCESS_BLUETOOTH);
}
});

Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
return false;
}
if (requestLocationServicePermission(fragment)) {
if (fragment.getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getActivity());

builder.setMessage(R.string.permission_bluetooth_info)
.setTitle(R.string.permission_bluetooth_info_title)
.setIcon(R.drawable.ic_preferences_about)
.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
fragment.requestPermissions(requiredPermissions, PERMISSIONS_REQUEST_ACCESS_BLUETOOTH);
}
});

Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
return false;
}
}

Expand Down Expand Up @@ -136,24 +134,20 @@ public void onClick(DialogInterface dialogInterface, int i) {
}

public static boolean requestReadPermission(final Fragment fragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (fragment.getContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
fragment.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_ACCESS_READ_STORAGE);
} else {
return true;
}
if (fragment.getContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
fragment.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_ACCESS_READ_STORAGE);
} else {
return true;
}

return false;
}

public static boolean requestWritePermission(final Fragment fragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (fragment.getContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
fragment.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE);
} else {
return true;
}
if (fragment.getContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
fragment.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE);
} else {
return true;
}

return false;
Expand Down

0 comments on commit be98e37

Please sign in to comment.