OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "device/bluetooth/bluetooth_device.h" |
| 14 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 15 #include "device/bluetooth/bluetooth_uuid.h" |
| 16 |
| 17 using device::BluetoothAdapter; |
| 18 using device::BluetoothDevice; |
| 19 using device::BluetoothGattConnection; |
| 20 |
| 21 namespace proximity_auth { |
| 22 |
| 23 BluetoothLowEnergyConnectionFinder::BluetoothLowEnergyConnectionFinder( |
| 24 const std::string& remote_service_uuid) |
| 25 : remote_service_uuid_(device::BluetoothUUID(remote_service_uuid)), |
| 26 connected_(false), |
| 27 weak_ptr_factory_(this) { |
| 28 } |
| 29 |
| 30 BluetoothLowEnergyConnectionFinder::~BluetoothLowEnergyConnectionFinder() { |
| 31 if (discovery_session_) { |
| 32 StopDiscoverySession(); |
| 33 } |
| 34 if (adapter_) { |
| 35 adapter_->RemoveObserver(this); |
| 36 adapter_ = NULL; |
| 37 } |
| 38 } |
| 39 |
| 40 void BluetoothLowEnergyConnectionFinder::Find( |
| 41 const BluetoothDevice::GattConnectionCallback& connection_callback) { |
| 42 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 43 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; |
| 44 return; |
| 45 } |
| 46 VLOG(1) << "Finding connection"; |
| 47 |
| 48 connection_callback_ = connection_callback; |
| 49 |
| 50 device::BluetoothAdapterFactory::GetAdapter( |
| 51 base::Bind(&BluetoothLowEnergyConnectionFinder::OnAdapterInitialized, |
| 52 weak_ptr_factory_.GetWeakPtr())); |
| 53 } |
| 54 |
| 55 void BluetoothLowEnergyConnectionFinder::Find( |
| 56 const ConnectionCallback& connection_callback) { |
| 57 NOTREACHED(); |
| 58 } |
| 59 |
| 60 void BluetoothLowEnergyConnectionFinder::DeviceAdded(BluetoothAdapter* adapter, |
| 61 BluetoothDevice* device) { |
| 62 if (device) { |
| 63 VLOG(1) << "New device found: " << device->GetName(); |
| 64 HandleDeviceAdded(device); |
| 65 } |
| 66 } |
| 67 |
| 68 void BluetoothLowEnergyConnectionFinder::OnAdapterInitialized( |
| 69 scoped_refptr<BluetoothAdapter> adapter) { |
| 70 VLOG(1) << "Adapter ready"; |
| 71 |
| 72 adapter_ = adapter; |
| 73 adapter_->AddObserver(this); |
| 74 |
| 75 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
| 76 for (auto iter = devices.begin(); iter != devices.end(); iter++) { |
| 77 HandleDeviceAdded((*iter)); |
| 78 } |
| 79 |
| 80 StartDiscoverySession(); |
| 81 } |
| 82 |
| 83 void BluetoothLowEnergyConnectionFinder::HandleDeviceAdded( |
| 84 BluetoothDevice* remote_device) { |
| 85 if (!connected_ && HasService(remote_device)) { |
| 86 CreateConnection(remote_device); |
| 87 } |
| 88 } |
| 89 |
| 90 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted( |
| 91 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 92 VLOG(1) << "Discovery session started"; |
| 93 discovery_session_ = discovery_session.Pass(); |
| 94 } |
| 95 |
| 96 void BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError() { |
| 97 VLOG(1) << "Error starting discovery session"; |
| 98 } |
| 99 |
| 100 void BluetoothLowEnergyConnectionFinder::StartDiscoverySession() { |
| 101 DCHECK(adapter_); |
| 102 if (discovery_session_ && discovery_session_->IsActive()) { |
| 103 VLOG(1) << "Discovery session already active"; |
| 104 return; |
| 105 } |
| 106 adapter_->StartDiscoverySession( |
| 107 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStarted, |
| 108 weak_ptr_factory_.GetWeakPtr()), |
| 109 base::Bind( |
| 110 &BluetoothLowEnergyConnectionFinder::OnStartDiscoverySessionError, |
| 111 weak_ptr_factory_.GetWeakPtr())); |
| 112 } |
| 113 |
| 114 void BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped() { |
| 115 VLOG(1) << "Discovery session stopped"; |
| 116 discovery_session_.reset(); |
| 117 } |
| 118 |
| 119 void BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError() { |
| 120 VLOG(1) << "Error stopping discovery session"; |
| 121 } |
| 122 |
| 123 void BluetoothLowEnergyConnectionFinder::StopDiscoverySession() { |
| 124 VLOG(1) << "Stopping discovery sesison"; |
| 125 |
| 126 if (!adapter_) { |
| 127 VLOG(1) << "Adapter not initialized"; |
| 128 return; |
| 129 } |
| 130 if (!discovery_session_ || !discovery_session_->IsActive()) { |
| 131 VLOG(1) << "No Active discovery session"; |
| 132 } |
| 133 |
| 134 discovery_session_->Stop( |
| 135 base::Bind(&BluetoothLowEnergyConnectionFinder::OnDiscoverySessionStopped, |
| 136 weak_ptr_factory_.GetWeakPtr()), |
| 137 base::Bind( |
| 138 &BluetoothLowEnergyConnectionFinder::OnStopDiscoverySessionError, |
| 139 weak_ptr_factory_.GetWeakPtr())); |
| 140 } |
| 141 |
| 142 bool BluetoothLowEnergyConnectionFinder::HasService( |
| 143 BluetoothDevice* remote_device) { |
| 144 if (remote_device) { |
| 145 std::vector<device::BluetoothUUID> uuids = remote_device->GetUUIDs(); |
| 146 for (auto iter = uuids.begin(); iter != uuids.end(); iter++) { |
| 147 if (remote_service_uuid_ == *iter) { |
| 148 return true; |
| 149 } |
| 150 } |
| 151 } |
| 152 return false; |
| 153 } |
| 154 |
| 155 void BluetoothLowEnergyConnectionFinder::OnCreateConnectionError( |
| 156 BluetoothDevice::ConnectErrorCode error_code) { |
| 157 VLOG(1) << "Error creating connection"; |
| 158 } |
| 159 |
| 160 void BluetoothLowEnergyConnectionFinder::OnConnectionCreated( |
| 161 scoped_ptr<BluetoothGattConnection> connection) { |
| 162 VLOG(1) << "Connection created"; |
| 163 connected_ = true; |
| 164 StopDiscoverySession(); |
| 165 connection_callback_.Run(connection.Pass()); |
| 166 } |
| 167 |
| 168 void BluetoothLowEnergyConnectionFinder::CreateConnection( |
| 169 device::BluetoothDevice* remote_device) { |
| 170 VLOG(1) << "SmartLock service found (" |
| 171 << remote_service_uuid_.canonical_value() << ")\n" |
| 172 << "device = " << remote_device->GetAddress() |
| 173 << ", name = " << remote_device->GetName(); |
| 174 remote_device->CreateGattConnection( |
| 175 base::Bind(&BluetoothLowEnergyConnectionFinder::OnConnectionCreated, |
| 176 weak_ptr_factory_.GetWeakPtr()), |
| 177 base::Bind(&BluetoothLowEnergyConnectionFinder::OnCreateConnectionError, |
| 178 weak_ptr_factory_.GetWeakPtr())); |
| 179 } |
| 180 |
| 181 } // namespace proximity_auth |
OLD | NEW |