| 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 #include "device/bluetooth/bluetooth_adapter_android.h" | 5 #include "device/bluetooth/bluetooth_adapter_android.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" |
| 10 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "device/bluetooth/android/wrappers.h" | 17 #include "device/bluetooth/android/wrappers.h" |
| 17 #include "device/bluetooth/bluetooth_advertisement.h" | 18 #include "device/bluetooth/bluetooth_advertisement.h" |
| 18 #include "device/bluetooth/bluetooth_device_android.h" | 19 #include "device/bluetooth/bluetooth_device_android.h" |
| 19 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 20 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" |
| 20 #include "jni/ChromeBluetoothAdapter_jni.h" | 21 #include "jni/ChromeBluetoothAdapter_jni.h" |
| 21 | 22 |
| 22 using base::android::AttachCurrentThread; | 23 using base::android::AttachCurrentThread; |
| 23 using base::android::ConvertJavaStringToUTF8; | 24 using base::android::ConvertJavaStringToUTF8; |
| 25 using base::android::AppendJavaStringArrayToStringVector; |
| 24 using base::android::JavaParamRef; | 26 using base::android::JavaParamRef; |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 // The poll interval in ms when there is no active discovery. This | 29 // The poll interval in ms when there is no active discovery. This |
| 28 // matches the max allowed advertisting interval for connectable | 30 // matches the max allowed advertisting interval for connectable |
| 29 // devices. | 31 // devices. |
| 30 enum { kPassivePollInterval = 11000 }; | 32 enum { kPassivePollInterval = 11000 }; |
| 31 // The poll interval in ms when there is an active discovery. | 33 // The poll interval in ms when there is an active discovery. |
| 32 enum { kActivePollInterval = 1000 }; | 34 enum { kActivePollInterval = 1000 }; |
| 33 // The delay in ms to wait before purging devices when a scan starts. | 35 // The delay in ms to wait before purging devices when a scan starts. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 MarkDiscoverySessionsAsInactive(); | 178 MarkDiscoverySessionsAsInactive(); |
| 177 } | 179 } |
| 178 | 180 |
| 179 void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan( | 181 void BluetoothAdapterAndroid::CreateOrUpdateDeviceOnScan( |
| 180 JNIEnv* env, | 182 JNIEnv* env, |
| 181 const JavaParamRef<jobject>& caller, | 183 const JavaParamRef<jobject>& caller, |
| 182 const JavaParamRef<jstring>& address, | 184 const JavaParamRef<jstring>& address, |
| 183 const JavaParamRef<jobject>& | 185 const JavaParamRef<jobject>& |
| 184 bluetooth_device_wrapper, // Java Type: bluetoothDeviceWrapper | 186 bluetooth_device_wrapper, // Java Type: bluetoothDeviceWrapper |
| 185 const JavaParamRef<jobjectArray>& | 187 const JavaParamRef<jobjectArray>& |
| 186 advertised_uuids) { // Java Type: List<ParcelUuid> | 188 advertised_uuids) { // Java Type: String[] |
| 187 std::string device_address = ConvertJavaStringToUTF8(env, address); | 189 std::string device_address = ConvertJavaStringToUTF8(env, address); |
| 188 DevicesMap::const_iterator iter = devices_.find(device_address); | 190 DevicesMap::const_iterator iter = devices_.find(device_address); |
| 189 | 191 |
| 192 bool is_new_device = false; |
| 193 std::unique_ptr<BluetoothDeviceAndroid> device_android_owner; |
| 194 BluetoothDeviceAndroid* device_android; |
| 195 |
| 190 if (iter == devices_.end()) { | 196 if (iter == devices_.end()) { |
| 191 // New device. | 197 // New device. |
| 192 BluetoothDeviceAndroid* device_android = | 198 is_new_device = true; |
| 193 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper); | 199 device_android_owner.reset( |
| 194 device_android->UpdateAdvertisedUUIDs(advertised_uuids); | 200 BluetoothDeviceAndroid::Create(this, bluetooth_device_wrapper)); |
| 195 device_android->UpdateTimestamp(); | 201 device_android = device_android_owner.get(); |
| 196 devices_.add(device_address, | 202 } else { |
| 197 std::unique_ptr<BluetoothDevice>(device_android)); | 203 // Existing device. |
| 204 device_android = static_cast<BluetoothDeviceAndroid*>(iter->second); |
| 205 } |
| 206 DCHECK(device_android); |
| 207 std::vector<std::string> advertised_uuids_strings; |
| 208 AppendJavaStringArrayToStringVector(env, advertised_uuids, |
| 209 &advertised_uuids_strings); |
| 210 |
| 211 BluetoothDevice::UUIDList advertised_bluetooth_uuids; |
| 212 for (std::string& uuid : advertised_uuids_strings) { |
| 213 advertised_bluetooth_uuids.push_back(BluetoothUUID(std::move(uuid))); |
| 214 } |
| 215 |
| 216 device_android->UpdateAdvertisementData(std::move(advertised_bluetooth_uuids), |
| 217 {} /* service_data */); |
| 218 |
| 219 if (is_new_device) { |
| 220 devices_.add(device_address, std::move(device_android_owner)); |
| 198 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 221 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 199 DeviceAdded(this, device_android)); | 222 DeviceAdded(this, device_android)); |
| 200 } else { | 223 } else { |
| 201 // Existing device. | |
| 202 BluetoothDeviceAndroid* device_android = | |
| 203 static_cast<BluetoothDeviceAndroid*>(iter->second); | |
| 204 device_android->UpdateTimestamp(); | |
| 205 device_android->UpdateAdvertisedUUIDs(advertised_uuids); | |
| 206 | |
| 207 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 224 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 208 DeviceChanged(this, device_android)); | 225 DeviceChanged(this, device_android)); |
| 209 } | 226 } |
| 210 } | 227 } |
| 211 | 228 |
| 212 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) { | 229 BluetoothAdapterAndroid::BluetoothAdapterAndroid() : weak_ptr_factory_(this) { |
| 213 } | 230 } |
| 214 | 231 |
| 215 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() { | 232 BluetoothAdapterAndroid::~BluetoothAdapterAndroid() { |
| 216 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction( | 233 Java_ChromeBluetoothAdapter_onBluetoothAdapterAndroidDestruction( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 if (num_discovery_sessions_ == 0) { | 292 if (num_discovery_sessions_ == 0) { |
| 276 VLOG(1) << "RemoveDiscoverySession: No scan in progress."; | 293 VLOG(1) << "RemoveDiscoverySession: No scan in progress."; |
| 277 NOTREACHED(); | 294 NOTREACHED(); |
| 278 } else { | 295 } else { |
| 279 --num_discovery_sessions_; | 296 --num_discovery_sessions_; |
| 280 session_removed = true; | 297 session_removed = true; |
| 281 if (num_discovery_sessions_ == 0) { | 298 if (num_discovery_sessions_ == 0) { |
| 282 VLOG(1) << "RemoveDiscoverySession: Now 0 sessions. Stopping scan."; | 299 VLOG(1) << "RemoveDiscoverySession: Now 0 sessions. Stopping scan."; |
| 283 session_removed = Java_ChromeBluetoothAdapter_stopScan( | 300 session_removed = Java_ChromeBluetoothAdapter_stopScan( |
| 284 AttachCurrentThread(), j_adapter_); | 301 AttachCurrentThread(), j_adapter_); |
| 302 for (const auto& device_id_object_pair : devices_) { |
| 303 device_id_object_pair.second->ClearAdvertisementData(); |
| 304 } |
| 285 } else { | 305 } else { |
| 286 VLOG(1) << "RemoveDiscoverySession: Now " | 306 VLOG(1) << "RemoveDiscoverySession: Now " |
| 287 << unsigned(num_discovery_sessions_) << " sessions."; | 307 << unsigned(num_discovery_sessions_) << " sessions."; |
| 288 } | 308 } |
| 289 } | 309 } |
| 290 | 310 |
| 291 if (session_removed) { | 311 if (session_removed) { |
| 292 callback.Run(); | 312 callback.Run(); |
| 293 } else { | 313 } else { |
| 294 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here. | 314 // TODO(scheib): Eventually wire the SCAN_FAILED result through to here. |
| 295 error_callback.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN); | 315 error_callback.Run(UMABluetoothDiscoverySessionOutcome::UNKNOWN); |
| 296 } | 316 } |
| 297 } | 317 } |
| 298 | 318 |
| 299 void BluetoothAdapterAndroid::SetDiscoveryFilter( | 319 void BluetoothAdapterAndroid::SetDiscoveryFilter( |
| 300 std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter, | 320 std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter, |
| 301 const base::Closure& callback, | 321 const base::Closure& callback, |
| 302 const DiscoverySessionErrorCallback& error_callback) { | 322 const DiscoverySessionErrorCallback& error_callback) { |
| 303 // TODO(scheib): Support filters crbug.com/490401 | 323 // TODO(scheib): Support filters crbug.com/490401 |
| 304 NOTIMPLEMENTED(); | 324 NOTIMPLEMENTED(); |
| 305 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED); | 325 error_callback.Run(UMABluetoothDiscoverySessionOutcome::NOT_IMPLEMENTED); |
| 306 } | 326 } |
| 307 | 327 |
| 308 void BluetoothAdapterAndroid::RemovePairingDelegateInternal( | 328 void BluetoothAdapterAndroid::RemovePairingDelegateInternal( |
| 309 device::BluetoothDevice::PairingDelegate* pairing_delegate) { | 329 device::BluetoothDevice::PairingDelegate* pairing_delegate) { |
| 310 } | 330 } |
| 311 | 331 |
| 312 } // namespace device | 332 } // namespace device |
| OLD | NEW |