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 18 matching lines...) Expand all Loading... | |
29 * Lifetime is controlled by device::BluetoothAdapterAndroid. | 29 * Lifetime is controlled by device::BluetoothAdapterAndroid. |
30 */ | 30 */ |
31 @JNINamespace("device") | 31 @JNINamespace("device") |
32 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 32 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
33 final class ChromeBluetoothAdapter extends BroadcastReceiver { | 33 final class ChromeBluetoothAdapter extends BroadcastReceiver { |
34 private static final String TAG = "Bluetooth"; | 34 private static final String TAG = "Bluetooth"; |
35 | 35 |
36 private long mNativeBluetoothAdapterAndroid; | 36 private long mNativeBluetoothAdapterAndroid; |
37 // mAdapter is final to ensure registerReceiver is followed by unregisterRec eiver. | 37 // mAdapter is final to ensure registerReceiver is followed by unregisterRec eiver. |
38 private final Wrappers.BluetoothAdapterWrapper mAdapter; | 38 private final Wrappers.BluetoothAdapterWrapper mAdapter; |
39 private int mNumDiscoverySessions; | |
40 private ScanCallback mScanCallback; | 39 private ScanCallback mScanCallback; |
41 | 40 |
42 // ------------------------------------------------------------------------- -------------------- | 41 // ------------------------------------------------------------------------- -------------------- |
43 // Construction and handler for C++ object destruction. | 42 // Construction and handler for C++ object destruction. |
44 | 43 |
45 /** | 44 /** |
46 * Constructs a ChromeBluetoothAdapter. | 45 * Constructs a ChromeBluetoothAdapter. |
47 * @param nativeBluetoothAdapterAndroid Is the associated C++ | 46 * @param nativeBluetoothAdapterAndroid Is the associated C++ |
48 * BluetoothAdapterAndroid pointer valu e. | 47 * BluetoothAdapterAndroid pointer valu e. |
49 * @param adapterWrapper Wraps the default android.bluetooth.BluetoothAdapte r, | 48 * @param adapterWrapper Wraps the default android.bluetooth.BluetoothAdapte r, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 } | 135 } |
137 | 136 |
138 // Implements BluetoothAdapterAndroid::IsDiscovering. | 137 // Implements BluetoothAdapterAndroid::IsDiscovering. |
139 @CalledByNative | 138 @CalledByNative |
140 private boolean isDiscovering() { | 139 private boolean isDiscovering() { |
141 return isPresent() && (mAdapter.isDiscovering() || mScanCallback != null ); | 140 return isPresent() && (mAdapter.isDiscovering() || mScanCallback != null ); |
142 } | 141 } |
143 | 142 |
144 // Implements BluetoothAdapterAndroid::AddDiscoverySession. | 143 // Implements BluetoothAdapterAndroid::AddDiscoverySession. |
145 @CalledByNative | 144 @CalledByNative |
146 private boolean addDiscoverySession() { | 145 private boolean addDiscoverySession() { |
ortuno
2016/06/15 17:20:16
This function seems less useful now. Make startSca
perja
2016/06/17 13:39:27
Done.
| |
147 if (!isPowered()) { | 146 return startScan(); |
148 Log.d(TAG, "addDiscoverySession: Fails: !isPowered"); | |
149 return false; | |
150 } | |
151 | |
152 mNumDiscoverySessions++; | |
153 Log.d(TAG, "addDiscoverySession: Now %d sessions.", mNumDiscoverySession s); | |
154 if (mNumDiscoverySessions > 1) { | |
155 return true; | |
156 } | |
157 | |
158 if (!startScan()) { | |
159 mNumDiscoverySessions--; | |
160 return false; | |
161 } | |
162 return true; | |
163 } | 147 } |
164 | 148 |
165 // Implements BluetoothAdapterAndroid::RemoveDiscoverySession. | 149 // Implements BluetoothAdapterAndroid::RemoveDiscoverySession. |
166 @CalledByNative | 150 @CalledByNative |
167 private boolean removeDiscoverySession() { | 151 private boolean removeDiscoverySession() { |
168 if (mNumDiscoverySessions == 0) { | 152 return stopScan(); |
169 assert false; | |
170 Log.w(TAG, "removeDiscoverySession: No scan in progress."); | |
171 return false; | |
172 } | |
173 | |
174 --mNumDiscoverySessions; | |
175 | |
176 if (mNumDiscoverySessions == 0) { | |
177 Log.d(TAG, "removeDiscoverySession: Now 0 sessions. Stopping scan.") ; | |
178 return stopScan(); | |
179 } | |
180 | |
181 Log.d(TAG, "removeDiscoverySession: Now %d sessions.", mNumDiscoverySess ions); | |
182 return true; | |
183 } | 153 } |
184 | 154 |
185 // ------------------------------------------------------------------------- -------------------- | 155 // ------------------------------------------------------------------------- -------------------- |
186 // Implementation details: | 156 // Implementation details: |
187 | 157 |
188 /** | 158 /** |
189 * @return true if Chromium has permission to scan for Bluetooth devices. | 159 * @return true if Chromium has permission to scan for Bluetooth devices. |
190 */ | 160 */ |
191 private boolean canScan() { | 161 private boolean canScan() { |
192 Wrappers.ContextWrapper context = mAdapter.getContext(); | 162 Wrappers.ContextWrapper context = mAdapter.getContext(); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 List<ParcelUuid> uuids = result.getScanRecord_getServiceUuids(); | 254 List<ParcelUuid> uuids = result.getScanRecord_getServiceUuids(); |
285 | 255 |
286 nativeCreateOrUpdateDeviceOnScan(mNativeBluetoothAdapterAndroid, | 256 nativeCreateOrUpdateDeviceOnScan(mNativeBluetoothAdapterAndroid, |
287 result.getDevice().getAddress(), result.getDevice(), uuids); | 257 result.getDevice().getAddress(), result.getDevice(), uuids); |
288 } | 258 } |
289 | 259 |
290 @Override | 260 @Override |
291 public void onScanFailed(int errorCode) { | 261 public void onScanFailed(int errorCode) { |
292 Log.w(TAG, "onScanFailed: %d", errorCode); | 262 Log.w(TAG, "onScanFailed: %d", errorCode); |
293 nativeOnScanFailed(mNativeBluetoothAdapterAndroid); | 263 nativeOnScanFailed(mNativeBluetoothAdapterAndroid); |
294 mNumDiscoverySessions = 0; | |
295 } | 264 } |
296 } | 265 } |
297 | 266 |
298 @Override | 267 @Override |
299 public void onReceive(Context context, Intent intent) { | 268 public void onReceive(Context context, Intent intent) { |
300 String action = intent.getAction(); | 269 String action = intent.getAction(); |
301 | 270 |
302 if (isPresent() && BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { | 271 if (isPresent() && BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { |
303 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, Bluetoo thAdapter.ERROR); | 272 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, Bluetoo thAdapter.ERROR); |
304 | 273 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss | 313 // 'Object' type must be used for |bluetoothDeviceWrapper| because inner cla ss |
345 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni. | 314 // Wrappers.BluetoothDeviceWrapper reference is not handled by jni_generator .py JavaToJni. |
346 // http://crbug.com/505554 | 315 // http://crbug.com/505554 |
347 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid, | 316 private native void nativeCreateOrUpdateDeviceOnScan(long nativeBluetoothAda pterAndroid, |
348 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids); | 317 String address, Object bluetoothDeviceWrapper, List<ParcelUuid> adve rtisedUuids); |
349 | 318 |
350 // Binds to BluetoothAdapterAndroid::nativeOnAdapterStateChanged | 319 // Binds to BluetoothAdapterAndroid::nativeOnAdapterStateChanged |
351 private native void nativeOnAdapterStateChanged( | 320 private native void nativeOnAdapterStateChanged( |
352 long nativeBluetoothAdapterAndroid, boolean powered); | 321 long nativeBluetoothAdapterAndroid, boolean powered); |
353 } | 322 } |
OLD | NEW |