Index: components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc |
diff --git a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63dc750ff1d2eb2c36241c0054ca79cb017a812b |
--- /dev/null |
+++ b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc |
@@ -0,0 +1,170 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h" |
+ |
+#include <string> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/logging.h" |
+#include "device/bluetooth/bluetooth_adapter_factory.h" |
+#include "device/bluetooth/bluetooth_device.h" |
+#include "device/bluetooth/bluetooth_discovery_session.h" |
+#include "device/bluetooth/bluetooth_uuid.h" |
+ |
+using device::BluetoothAdapter; |
+using device::BluetoothDevice; |
+ |
+namespace proximity_auth { |
+ |
+BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
+ const std::string& remote_service_uuid) |
+ : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
+ weak_ptr_factory_(this) { |
+} |
+ |
+BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
+ if (discovery_session_.get()) { |
+ StopDiscoverySession(); |
+ } |
+ if (adapter_) { |
+ adapter_->RemoveObserver(this); |
+ adapter_ = NULL; |
+ } |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::Find( |
+ const BluetoothDevice::GattConnectionCallback& connection_callback) { |
+ if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
+ VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; |
+ return; |
+ } |
+ VLOG(1) << "Finding connection"; |
+ |
+ connection_callback_ = connection_callback; |
+ |
+ device::BluetoothAdapterFactory::GetAdapter( |
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::Find( |
+ const ConnectionCallback& connection_callback) { |
+ NOTREACHED(); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
+ BluetoothDevice* device) { |
+ if (device) { |
+ VLOG(1) << "New device found: " << device->GetName(); |
+ HandleDeviceAdded(device); |
+ } |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( |
+ scoped_refptr<BluetoothAdapter> adapter) { |
+ VLOG(1) << "Adapter ready"; |
+ |
+ adapter_ = adapter; |
+ adapter_->AddObserver(this); |
+ |
+ std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
+ for (auto iter = devices.begin(); iter != devices.end(); iter++) { |
+ HandleDeviceAdded((*iter)); |
+ } |
+ |
+ StartDiscoverySession(); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::HandleDeviceAdded( |
+ BluetoothDevice* remote_device) { |
+ if (HasService(remote_device)) { |
+ CreateConnection(remote_device); |
msarda
2015/04/24 14:12:16
This seems to potentially create multiple concurre
sacomoto
2015/04/24 14:56:22
You are right. Modified.
|
+ } |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( |
+ scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
+ VLOG(1) << "Discovery session started"; |
+ discovery_session_ = discovery_session.Pass(); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { |
+ VLOG(1) << "Error starting discovery session"; |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { |
+ DCHECK(adapter_); |
+ if (discovery_session_.get() && discovery_session_->IsActive()) { |
+ VLOG(1) << "Discovery session already active"; |
+ return; |
+ } |
+ adapter_->StartDiscoverySession( |
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, |
+ weak_ptr_factory_.GetWeakPtr()), |
+ base::Bind( |
+ &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() { |
+ VLOG(1) << "Discovery session stopped"; |
+ discovery_session_.reset(); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError() { |
+ VLOG(1) << "Error stopping discovery session"; |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { |
+ VLOG(1) << "Stopping discovery sesison"; |
+ |
+ if (!adapter_.get()) { |
+ VLOG(1) << "Adapter not initialized"; |
+ return; |
+ } |
+ if (!discovery_session_.get() || !discovery_session_->IsActive()) { |
+ VLOG(1) << "No Active discovery session"; |
+ } |
+ |
+ discovery_session_->Stop( |
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped, |
+ weak_ptr_factory_.GetWeakPtr()), |
+ base::Bind( |
+ &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+bool BluetoothLowEnergyConnectionFinder::HasService( |
+ BluetoothDevice* remote_device) { |
+ if (remote_device) { |
msarda
2015/04/24 14:12:16
Can this actually be nullptr? If not, then I think
sacomoto
2015/04/24 14:56:22
I think it can. When the observer interface method
|
+ std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); |
+ for (auto iter = uuids.begin(); iter != uuids.end(); iter++) { |
+ if (remote_service_uuid_ == *iter) { |
+ return true; |
+ } |
+ } |
+ } |
+ return false; |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( |
+ BluetoothDevice::ConnectErrorCode error_code) { |
+ VLOG(1) << "Error creating connection"; |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::CreateConnection( |
+ device::BluetoothDevice* remote_device) { |
+ VLOG(1) << "SmartLock service found (" |
+ << remote_service_uuid_.canonical_value() << ")\n" |
+ << "device = " << remote_device->GetAddress() |
+ << ", name = " << remote_device->GetName(); |
+ remote_device->CreateGattConnection( |
+ connection_callback_, |
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+} // namespace proximity_auth |