OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.bluetooth; | 5 package org.chromium.device.bluetooth; |
6 | 6 |
7 import android.Manifest; | 7 import android.Manifest; |
8 import android.annotation.TargetApi; | 8 import android.annotation.TargetApi; |
9 import android.bluetooth.BluetoothAdapter; | 9 import android.bluetooth.BluetoothAdapter; |
10 import android.bluetooth.le.ScanSettings; | 10 import android.bluetooth.le.ScanSettings; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 Wrappers.ContextWrapper context = mAdapter.getContext(); | 175 Wrappers.ContextWrapper context = mAdapter.getContext(); |
176 return context.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATIO N) | 176 return context.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATIO N) |
177 || context.checkPermission(Manifest.permission.ACCESS_FINE_LOCAT ION); | 177 || context.checkPermission(Manifest.permission.ACCESS_FINE_LOCAT ION); |
178 } | 178 } |
179 | 179 |
180 /** | 180 /** |
181 * Starts a Low Energy scan. | 181 * Starts a Low Energy scan. |
182 * @return True on success. | 182 * @return True on success. |
183 */ | 183 */ |
184 private boolean startScan() { | 184 private boolean startScan() { |
185 Wrappers.BluetoothLeScannerWrapper scanner = mAdapter.getBluetoothLeScan ner(); | 185 Wrappers.BluetoothLeScannerWrapper scanner = mAdapter.getBluetoothLeScan ner(); |
scheib
2016/01/21 05:33:51
scanner may be null.
| |
186 | 186 |
187 if (!canScan()) { | 187 if (!canScan()) { |
188 return false; | 188 return false; |
189 } | 189 } |
190 | 190 |
191 // scanMode note: SCAN_FAILED_FEATURE_UNSUPPORTED is caused (at least on some devices) if | 191 // scanMode note: SCAN_FAILED_FEATURE_UNSUPPORTED is caused (at least on some devices) if |
192 // setReportDelay() is used or if SCAN_MODE_LOW_LATENCY isn't used. | 192 // setReportDelay() is used or if SCAN_MODE_LOW_LATENCY isn't used. |
193 int scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY; | 193 int scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY; |
194 | 194 |
195 assert mScanCallback == null; | 195 assert mScanCallback == null; |
196 mScanCallback = new ScanCallback(); | 196 mScanCallback = new ScanCallback(); |
197 | 197 |
198 try { | 198 try { |
199 scanner.startScan(null /* filters */, scanMode, mScanCallback); | 199 scanner.startScan(null /* filters */, scanMode, mScanCallback); |
200 } catch (IllegalArgumentException e) { | 200 } catch (IllegalArgumentException e) { |
201 Log.e(TAG, "Cannot start scan: " + e); | 201 Log.e(TAG, "Cannot start scan: " + e); |
202 return false; | 202 return false; |
203 } catch (IllegalStateException e) { | |
scheib
2016/01/21 05:33:51
Add a test:
- In Fakes.java have FakeBluetoothLeSc
| |
204 Log.e(TAG, "Adapter is off. Cannot start scan: " + e); | |
205 return false; | |
203 } | 206 } |
204 return true; | 207 return true; |
205 } | 208 } |
206 | 209 |
207 /** | 210 /** |
208 * Stops the Low Energy scan. | 211 * Stops the Low Energy scan. |
209 * @return True if a scan was in progress. | 212 * @return True if a scan was in progress. |
210 */ | 213 */ |
211 private boolean stopScan() { | 214 private boolean stopScan() { |
212 if (mScanCallback == null) { | 215 if (mScanCallback == null) { |
213 return false; | 216 return false; |
214 } | 217 } |
215 try { | 218 try { |
216 mAdapter.getBluetoothLeScanner().stopScan(mScanCallback); | 219 mAdapter.getBluetoothLeScanner().stopScan(mScanCallback); |
217 } catch (IllegalArgumentException e) { | 220 } catch (IllegalArgumentException e) { |
218 Log.e(TAG, "Cannot stop scan: " + e); | 221 Log.e(TAG, "Cannot stop scan: " + e); |
219 mScanCallback = null; | 222 mScanCallback = null; |
220 return false; | 223 return false; |
224 } catch (IllegalStateException e) { | |
225 Log.e(TAG, "Adapter is off. Cannot stop scan: " + e); | |
226 mScanCallback = null; | |
227 return false; | |
221 } | 228 } |
222 mScanCallback = null; | 229 mScanCallback = null; |
223 return true; | 230 return true; |
224 } | 231 } |
225 | 232 |
226 /** | 233 /** |
227 * Implements callbacks used during a Low Energy scan by notifying upon | 234 * Implements callbacks used during a Low Energy scan by notifying upon |
228 * devices discovered or detecting a scan failure. | 235 * devices discovered or detecting a scan failure. |
229 */ | 236 */ |
230 private class ScanCallback extends Wrappers.ScanCallbackWrapper { | 237 private class ScanCallback extends Wrappers.ScanCallbackWrapper { |
(...skipping 27 matching lines...) Expand all Loading... | |
258 // Binds to BluetoothAdapterAndroid::OnScanFailed. | 265 // Binds to BluetoothAdapterAndroid::OnScanFailed. |
259 private native void nativeOnScanFailed(long nativeBluetoothAdapterAndroid); | 266 private native void nativeOnScanFailed(long nativeBluetoothAdapterAndroid); |
260 | 267 |
261 // Binds to BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan. | 268 // Binds to BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan. |
262 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss | 269 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss |
263 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni. | 270 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni. |
264 // http://crbug.com/505554 | 271 // http://crbug.com/505554 |
265 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid, | 272 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid, |
266 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids); | 273 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids); |
267 } | 274 } |
OLD | NEW |