|
3 | 3 | import android.Manifest; |
4 | 4 | import android.bluetooth.BluetoothAdapter; |
5 | 5 | import android.bluetooth.BluetoothDevice; |
| 6 | +import android.os.Build; |
6 | 7 | import android.os.Bundle; |
7 | 8 | import android.support.annotation.NonNull; |
8 | 9 | import android.support.v7.app.AppCompatActivity; |
@@ -119,22 +120,7 @@ public boolean onCreateOptionsMenu(final Menu menu) { |
119 | 120 | public boolean onOptionsItemSelected(final MenuItem item) { |
120 | 121 | switch (item.getItemId()) { |
121 | 122 | 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(); |
138 | 124 | break; |
139 | 125 | case R.id.menu_stop: |
140 | 126 | mScanner.scanLeDevice(-1, false); |
@@ -174,17 +160,44 @@ public void onResume() { |
174 | 160 | invalidateOptionsMenu(); |
175 | 161 | } |
176 | 162 |
|
| 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 | + |
177 | 190 | 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(); |
180 | 193 | mDeviceStore.clear(); |
181 | 194 | updateItemCount(0); |
182 | 195 |
|
183 | 196 | mRecyclerAdapter = new DeviceRecyclerAdapter(mCore); |
184 | 197 | mList.setAdapter(mRecyclerAdapter); |
185 | 198 |
|
186 | 199 | mBluetoothUtils.askUserToEnableBluetoothIfNeeded(); |
187 | | - if (mIsBluetoothOn && mIsBluetoothLePresent) { |
| 200 | + if (isBluetoothOn && isBluetoothLePresent) { |
188 | 201 | mScanner.scanLeDevice(-1, true); |
189 | 202 | invalidateOptionsMenu(); |
190 | 203 | } |
|
0 commit comments