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 |
index d911388fa5616dee2aa82c4d5944498a3d48d8b7..f158ab427a6b9c44aa4cd21a8463a8eea6154a9a 100644 |
--- a/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc |
+++ b/components/proximity_auth/ble/bluetooth_low_energy_connection_finder.cc |
@@ -10,6 +10,8 @@ |
#include "base/bind_helpers.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
+#include "components/proximity_auth/ble/bluetooth_low_energy_connection.h" |
+#include "components/proximity_auth/connection.h" |
#include "device/bluetooth/bluetooth_adapter_factory.h" |
#include "device/bluetooth/bluetooth_device.h" |
#include "device/bluetooth/bluetooth_discovery_session.h" |
@@ -23,8 +25,13 @@ using device::BluetoothDiscoveryFilter; |
namespace proximity_auth { |
BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
- const std::string& remote_service_uuid) |
+ const std::string& remote_service_uuid, |
+ const std::string& to_peripheral_char_uuid, |
+ const std::string& from_peripheral_char_uuid) |
: remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
+ to_peripheral_char_uuid_(device::BluetoothUUID(to_peripheral_char_uuid)), |
+ from_peripheral_char_uuid_( |
+ device::BluetoothUUID(from_peripheral_char_uuid)), |
connected_(false), |
weak_ptr_factory_(this) { |
} |
@@ -33,6 +40,12 @@ BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
if (discovery_session_) { |
StopDiscoverySession(); |
} |
+ |
+ if (connection_) { |
+ connection_->RemoveObserver(this); |
+ connection_.reset(); |
+ } |
+ |
if (adapter_) { |
adapter_->RemoveObserver(this); |
adapter_ = NULL; |
@@ -40,7 +53,7 @@ BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
} |
void BluetoothLowEnergyConnectionFinder::Find( |
- const BluetoothDevice::GattConnectionCallback& connection_callback) { |
+ const ConnectionCallback& connection_callback) { |
if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; |
return; |
@@ -54,11 +67,6 @@ void BluetoothLowEnergyConnectionFinder::Find( |
weak_ptr_factory_.GetWeakPtr())); |
} |
-void BluetoothLowEnergyConnectionFinder::Find( |
- const ConnectionCallback& connection_callback) { |
- NOTREACHED(); |
-} |
- |
void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
BluetoothDevice* device) { |
DCHECK(device); |
@@ -86,7 +94,7 @@ void BluetoothLowEnergyConnectionFinder::HandleDeviceUpdated( |
if (HasService(device)) { |
VLOG(1) << "Connecting to device " << device->GetAddress(); |
pending_connections_.insert(device); |
- CreateConnection(device); |
+ CreateGattConnection(device); |
} |
} |
@@ -193,47 +201,49 @@ bool BluetoothLowEnergyConnectionFinder::HasService( |
return false; |
} |
-void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( |
+void BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError( |
std::string device_address, |
BluetoothDevice::ConnectErrorCode error_code) { |
VLOG(1) << "Error creating connection to device " << device_address |
<< " : error code = " << error_code; |
} |
-void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( |
- scoped_ptr<BluetoothGattConnection> connection) { |
+void BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated( |
+ scoped_ptr<BluetoothGattConnection> gatt_connection) { |
if (connected_) { |
- CloseConnection(connection.Pass()); |
+ CloseGattConnection(gatt_connection.Pass()); |
return; |
} |
VLOG(1) << "Connection created"; |
connected_ = true; |
pending_connections_.clear(); |
- if (!connection_callback_.is_null()) { |
- connection_callback_.Run(connection.Pass()); |
- connection_callback_.Reset(); |
- } |
+ |
+ connection_ = CreateConnection(gatt_connection.Pass()); |
+ connection_->AddObserver(this); |
+ |
StopDiscoverySession(); |
} |
-void BluetoothLowEnergyConnectionFinder::CreateConnection( |
+void BluetoothLowEnergyConnectionFinder::CreateGattConnection( |
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( |
- base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, |
+ base::Bind(&BluetoothLowEnergyConnectionFinder::OnGattConnectionCreated, |
weak_ptr_factory_.GetWeakPtr()), |
- base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, |
- weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); |
+ base::Bind( |
+ &BluetoothLowEnergyConnectionFinder::OnCreateGattConnectionError, |
+ weak_ptr_factory_.GetWeakPtr(), remote_device->GetAddress())); |
} |
-void BluetoothLowEnergyConnectionFinder::CloseConnection( |
- scoped_ptr<device::BluetoothGattConnection> connection) { |
- std::string device_address = connection->GetDeviceAddress(); |
- connection.reset(); |
+void BluetoothLowEnergyConnectionFinder::CloseGattConnection( |
+ scoped_ptr<device::BluetoothGattConnection> gatt_connection) { |
+ DCHECK(gatt_connection); |
+ std::string device_address = gatt_connection->GetDeviceAddress(); |
+ gatt_connection.reset(); |
BluetoothDevice* device = adapter_->GetDevice(device_address); |
if (device) { |
DCHECK(HasService(device)); |
@@ -242,4 +252,27 @@ void BluetoothLowEnergyConnectionFinder::CloseConnection( |
} |
} |
+scoped_ptr<Connection> BluetoothLowEnergyConnectionFinder::CreateConnection( |
+ scoped_ptr<BluetoothGattConnection> gatt_connection) { |
+ RemoteDevice remote_device; |
+ remote_device.bluetooth_address = gatt_connection->GetDeviceAddress(); |
+ |
+ return make_scoped_ptr(new BluetoothLowEnergyConnection( |
+ remote_device, adapter_, remote_service_uuid_, to_peripheral_char_uuid_, |
+ from_peripheral_char_uuid_, gatt_connection.Pass())); |
+} |
+ |
+void BluetoothLowEnergyConnectionFinder::OnConnectionStatusChanged( |
+ Connection* connection, |
+ Connection::Status old_status, |
+ Connection::Status new_status) { |
+ DCHECK_EQ(connection, connection_.get()); |
+ |
+ if (!connection_callback_.is_null() && connection_->IsConnected()) { |
+ connection_->RemoveObserver(this); |
+ connection_callback_.Run(connection_.Pass()); |
+ connection_callback_.Reset(); |
+ } |
+} |
+ |
} // namespace proximity_auth |