Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: device/bluetooth/android/java/src/org/chromium/device/bluetooth/ChromeBluetoothAdapter.java

Issue 1610053005: bluetooth: android: Fix a couple of crashes when adapter is turned on/off. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased on latest master Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 private boolean isPresent() { 104 private boolean isPresent() {
105 return mAdapter != null; 105 return mAdapter != null;
106 } 106 }
107 107
108 // Implements BluetoothAdapterAndroid::IsPowered. 108 // Implements BluetoothAdapterAndroid::IsPowered.
109 @CalledByNative 109 @CalledByNative
110 private boolean isPowered() { 110 private boolean isPowered() {
111 return isPresent() && mAdapter.isEnabled(); 111 return isPresent() && mAdapter.isEnabled();
112 } 112 }
113 113
114 // Implements BluetoothAdapterAndroid::SetPowered.
115 @CalledByNative
116 private boolean setPowered(boolean powered) {
117 if (powered) {
118 return isPresent() && mAdapter.enable();
119 } else {
120 return isPresent() && mAdapter.disable();
121 }
122 }
123
114 // Implements BluetoothAdapterAndroid::IsDiscoverable. 124 // Implements BluetoothAdapterAndroid::IsDiscoverable.
115 @CalledByNative 125 @CalledByNative
116 private boolean isDiscoverable() { 126 private boolean isDiscoverable() {
117 return isPresent() 127 return isPresent()
118 && mAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTA BLE_DISCOVERABLE; 128 && mAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTA BLE_DISCOVERABLE;
119 } 129 }
120 130
121 // Implements BluetoothAdapterAndroid::IsDiscovering. 131 // Implements BluetoothAdapterAndroid::IsDiscovering.
122 @CalledByNative 132 @CalledByNative
123 private boolean isDiscovering() { 133 private boolean isDiscovering() {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 || context.checkPermission(Manifest.permission.ACCESS_FINE_LOCAT ION); 187 || context.checkPermission(Manifest.permission.ACCESS_FINE_LOCAT ION);
178 } 188 }
179 189
180 /** 190 /**
181 * Starts a Low Energy scan. 191 * Starts a Low Energy scan.
182 * @return True on success. 192 * @return True on success.
183 */ 193 */
184 private boolean startScan() { 194 private boolean startScan() {
185 Wrappers.BluetoothLeScannerWrapper scanner = mAdapter.getBluetoothLeScan ner(); 195 Wrappers.BluetoothLeScannerWrapper scanner = mAdapter.getBluetoothLeScan ner();
186 196
197 if (scanner == null) {
198 return false;
199 }
200
187 if (!canScan()) { 201 if (!canScan()) {
188 return false; 202 return false;
189 } 203 }
190 204
191 // scanMode note: SCAN_FAILED_FEATURE_UNSUPPORTED is caused (at least on some devices) if 205 // 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. 206 // setReportDelay() is used or if SCAN_MODE_LOW_LATENCY isn't used.
193 int scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY; 207 int scanMode = ScanSettings.SCAN_MODE_LOW_LATENCY;
194 208
195 assert mScanCallback == null; 209 assert mScanCallback == null;
196 mScanCallback = new ScanCallback(); 210 mScanCallback = new ScanCallback();
197 211
198 try { 212 try {
199 scanner.startScan(null /* filters */, scanMode, mScanCallback); 213 scanner.startScan(null /* filters */, scanMode, mScanCallback);
200 } catch (IllegalArgumentException e) { 214 } catch (IllegalArgumentException e) {
201 Log.e(TAG, "Cannot start scan: " + e); 215 Log.e(TAG, "Cannot start scan: " + e);
216 mScanCallback = null;
217 return false;
218 } catch (IllegalStateException e) {
219 Log.e(TAG, "Adapter is off. Cannot start scan: " + e);
220 mScanCallback = null;
202 return false; 221 return false;
203 } 222 }
204 return true; 223 return true;
205 } 224 }
206 225
207 /** 226 /**
208 * Stops the Low Energy scan. 227 * Stops the Low Energy scan.
209 * @return True if a scan was in progress. 228 * @return True if a scan was in progress.
210 */ 229 */
211 private boolean stopScan() { 230 private boolean stopScan() {
212 if (mScanCallback == null) { 231 if (mScanCallback == null) {
213 return false; 232 return false;
214 } 233 }
234
215 try { 235 try {
216 mAdapter.getBluetoothLeScanner().stopScan(mScanCallback); 236 Wrappers.BluetoothLeScannerWrapper scanner = mAdapter.getBluetoothLe Scanner();
237 if (scanner != null) {
238 scanner.stopScan(mScanCallback);
239 }
217 } catch (IllegalArgumentException e) { 240 } catch (IllegalArgumentException e) {
218 Log.e(TAG, "Cannot stop scan: " + e); 241 Log.e(TAG, "Cannot stop scan: " + e);
219 mScanCallback = null; 242 } catch (IllegalStateException e) {
220 return false; 243 Log.e(TAG, "Adapter is off. Cannot stop scan: " + e);
221 } 244 }
222 mScanCallback = null; 245 mScanCallback = null;
223 return true; 246 return true;
224 } 247 }
225 248
226 /** 249 /**
227 * Implements callbacks used during a Low Energy scan by notifying upon 250 * Implements callbacks used during a Low Energy scan by notifying upon
228 * devices discovered or detecting a scan failure. 251 * devices discovered or detecting a scan failure.
229 */ 252 */
230 private class ScanCallback extends Wrappers.ScanCallbackWrapper { 253 private class ScanCallback extends Wrappers.ScanCallbackWrapper {
(...skipping 27 matching lines...) Expand all
258 // Binds to BluetoothAdapterAndroid::OnScanFailed. 281 // Binds to BluetoothAdapterAndroid::OnScanFailed.
259 private native void nativeOnScanFailed(long nativeBluetoothAdapterAndroid); 282 private native void nativeOnScanFailed(long nativeBluetoothAdapterAndroid);
260 283
261 // Binds to BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan. 284 // Binds to BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan.
262 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss 285 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss
263 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni. 286 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni.
264 // http://crbug.com/505554 287 // http://crbug.com/505554
265 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid, 288 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid,
266 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids); 289 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids);
267 } 290 }
OLDNEW
« no previous file with comments | « no previous file | device/bluetooth/android/java/src/org/chromium/device/bluetooth/Wrappers.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698