Skip to content

Commit ebc44c7

Browse files
committed
App no longer tries to ask for COARSE_LOCATION permission pre API 23 as it is not needed
1 parent f2f9ac3 commit ebc44c7

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

sample_app/src/main/java/uk/co/alt236/btlescan/ui/main/MainActivity.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.Manifest;
44
import android.bluetooth.BluetoothAdapter;
55
import android.bluetooth.BluetoothDevice;
6+
import android.os.Build;
67
import android.os.Bundle;
78
import android.support.annotation.NonNull;
89
import android.support.v7.app.AppCompatActivity;
@@ -119,22 +120,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
119120
public boolean onOptionsItemSelected(final MenuItem item) {
120121
switch (item.getItemId()) {
121122
case R.id.menu_scan:
122-
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
123-
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, new PermissionsResultAction() {
124-
125-
@Override
126-
public void onGranted() {
127-
startScan();
128-
}
129-
130-
@Override
131-
public void onDenied(String permission) {
132-
Toast.makeText(MainActivity.this,
133-
R.string.permission_not_granted_coarse_location,
134-
Toast.LENGTH_SHORT)
135-
.show();
136-
}
137-
});
123+
startScanPrepare();
138124
break;
139125
case R.id.menu_stop:
140126
mScanner.scanLeDevice(-1, false);
@@ -174,17 +160,44 @@ public void onResume() {
174160
invalidateOptionsMenu();
175161
}
176162

163+
164+
private void startScanPrepare() {
165+
//
166+
// The COARSE_LOCATION permission is only needed after API 23 to do a BTLE scan
167+
//
168+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
169+
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
170+
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, new PermissionsResultAction() {
171+
172+
@Override
173+
public void onGranted() {
174+
startScan();
175+
}
176+
177+
@Override
178+
public void onDenied(String permission) {
179+
Toast.makeText(MainActivity.this,
180+
R.string.permission_not_granted_coarse_location,
181+
Toast.LENGTH_SHORT)
182+
.show();
183+
}
184+
});
185+
} else {
186+
startScan();
187+
}
188+
}
189+
177190
private void startScan() {
178-
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
179-
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
191+
final boolean isBluetoothOn = mBluetoothUtils.isBluetoothOn();
192+
final boolean isBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
180193
mDeviceStore.clear();
181194
updateItemCount(0);
182195

183196
mRecyclerAdapter = new DeviceRecyclerAdapter(mCore);
184197
mList.setAdapter(mRecyclerAdapter);
185198

186199
mBluetoothUtils.askUserToEnableBluetoothIfNeeded();
187-
if (mIsBluetoothOn && mIsBluetoothLePresent) {
200+
if (isBluetoothOn && isBluetoothLePresent) {
188201
mScanner.scanLeDevice(-1, true);
189202
invalidateOptionsMenu();
190203
}

0 commit comments

Comments
 (0)