Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 12 bluetooth support #892

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
package org.envirocar.app.views.obdselection;

import android.Manifest;
import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.appcompat.widget.Toolbar;
import android.view.MenuItem;
Expand All @@ -34,20 +37,24 @@
import org.envirocar.app.injection.BaseInjectorActivity;
import org.envirocar.core.events.bluetooth.BluetoothStateChangedEvent;
import org.envirocar.core.logging.Logger;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

/**
* @dewall
*/
public class OBDSelectionActivity extends BaseInjectorActivity implements
OBDSelectionFragment.ShowSnackbarListener {
OBDSelectionFragment.ShowSnackbarListener,EasyPermissions.PermissionCallbacks {
private static final Logger LOGGER = Logger.getLogger(OBDSelectionActivity.class);

@Inject
Expand Down Expand Up @@ -88,14 +95,17 @@ protected void onCreate(Bundle savedInstanceState) {
if (mOBDSelectionFragment == null)
mOBDSelectionFragment = new OBDSelectionFragment();

checkAndRequestPermissions();
// Setup the bluetooth toolbar
setupBluetoothSwitch();
}

private void navigateToOBDSelection(){
// And set the fragment in the layout container.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.activity_obd_selection_layout_container, mOBDSelectionFragment)
.commit();

// Setup the bluetooth toolbar
setupBluetoothSwitch();
}

private void setupBluetoothSwitch() {
Expand Down Expand Up @@ -163,4 +173,62 @@ private void setSwitchState(float value) {
}
}

private final int BLUETOOTH_PERMISSIONS = 1;

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

EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
}

public void checkAndRequestPermissions() {
String[] perms;
if (android.os.Build.VERSION.SDK_INT >= 31) {
perms = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
};
}
else{
perms = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
}

if (EasyPermissions.hasPermissions(this, perms)){
// if all permissions are granted, navigate to OBDSelectionFragment.
navigateToOBDSelection();
}
else{
// Dialog requesting the user for location permission.
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, BLUETOOTH_PERMISSIONS, perms)
.setRationale(R.string.location_permission_to_discover_newdevices)
.setPositiveButtonText(R.string.grant_permissions)
.setNegativeButtonText(R.string.cancel)
.setTheme(R.style.MaterialDialog)
.build());
}
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull @NotNull List<String> perms) {
// if location permissions are granted, navigate to OBDSelectionFragment.
if (requestCode == BLUETOOTH_PERMISSIONS) {
navigateToOBDSelection();
showSnackbar(getString(R.string.location_permission_granted));
}
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull @NotNull List<String> perms) {
// if permissions are not granted, show toast.
if (requestCode == BLUETOOTH_PERMISSIONS) {
showSnackbar(getString(R.string.location_permission_denied));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.envirocar.app.views.obdselection;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.CountDownTimer;
Expand Down Expand Up @@ -47,7 +46,6 @@
import org.envirocar.core.logging.Logger;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;
Expand All @@ -59,16 +57,13 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;


/**
* TODO JavaDoc
*
* @author dewall
*/
public class OBDSelectionFragment extends BaseInjectorFragment implements EasyPermissions.PermissionCallbacks {
public class OBDSelectionFragment extends BaseInjectorFragment {
private static final Logger LOGGER = Logger.getLogger(OBDSelectionFragment.class);

@Override
Expand Down Expand Up @@ -136,14 +131,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
// // TODO: very ugly... Instead a dynamic LinearLayout should be used.
// setDynamicListHeight(mNewDevicesListView);
// setDynamicListHeight(mPairedDevicesListView);

return contentView;
}

@Override
public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
checkAndRequestPermissions();
startBluetoothDiscovery();
}

@Override
Expand All @@ -166,7 +160,6 @@ public void onBluetoothStateChangedEvent(BluetoothStateChangedEvent event) {
@OnClick(R.id.activity_obd_selection_layout_rescan_bluetooth)
protected void rediscover() {
mBluetoothHandler.stopBluetoothDeviceDiscovery();
checkAndRequestPermissions();
}

/**
Expand All @@ -188,65 +181,6 @@ private void updateContentView() {
}
}

private final int BLUETOOTH_PERMISSIONS = 1;

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

EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
}

public void checkAndRequestPermissions() {
String[] perms;
if (android.os.Build.VERSION.SDK_INT >= 31) {
perms = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
};
}
else{
perms = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
}

if (EasyPermissions.hasPermissions(getContext(), perms)){
// if all permissions are granted, start bluetooth discovery.
startBluetoothDiscovery();
}
else{
// Dialog requesting the user for location permission.
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, BLUETOOTH_PERMISSIONS, perms)
.setRationale(R.string.location_permission_to_discover_newdevices)
.setPositiveButtonText(R.string.grant_permissions)
.setNegativeButtonText(R.string.cancel)
.setTheme(R.style.MaterialDialog)
.build());
}
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull @NotNull List<String> perms) {
// if location permissions are granted, start Bluetooth discovery.
if (requestCode == BLUETOOTH_PERMISSIONS) {
startBluetoothDiscovery();
showSnackbar(getString(R.string.location_permission_granted));
}
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull @NotNull List<String> perms) {
// if permissions are not granted, show toast.
if (requestCode == BLUETOOTH_PERMISSIONS) {
showSnackbar(getString(R.string.location_permission_denied));
}
}

@Override
public void onResume() {
super.onResume();
Expand Down