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 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_LOW_ENERGY_CONNECTION_FINDER_H |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "components/proximity_auth/connection_finder.h" |
| 16 #include "device/bluetooth/bluetooth_adapter.h" |
| 17 #include "device/bluetooth/bluetooth_device.h" |
| 18 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 19 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 20 |
| 21 namespace proximity_auth { |
| 22 |
| 23 // This ConnectionFinder implementation is specialized in finding a Bluetooth |
| 24 // Low Energy remote device. |
| 25 class BluetoothLowEnergyConnectionFinder |
| 26 : public ConnectionFinder, |
| 27 public device::BluetoothAdapter::Observer { |
| 28 public: |
| 29 BluetoothLowEnergyConnectionFinder(const std::string& remote_service_uuid); |
| 30 ~BluetoothLowEnergyConnectionFinder() override; |
| 31 |
| 32 // Finds a connection the remote device, only the first one is functional. |
| 33 void Find(const device::BluetoothDevice::GattConnectionCallback& |
| 34 connection_callback); |
| 35 void Find(const ConnectionCallback& connection_callback) override; |
| 36 |
| 37 protected: |
| 38 // Observer for device::BluetoothAdapter::Observer |
| 39 void DeviceAdded(device::BluetoothAdapter* adapter, |
| 40 device::BluetoothDevice* device) override; |
| 41 |
| 42 private: |
| 43 // Callback to be called when the Bluetooth adapter is initialized. |
| 44 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
| 45 |
| 46 // Checks if |remote_device| contains |remote_service_uuid| and creates a |
| 47 // connection in that case. |
| 48 void HandleDeviceAdded(device::BluetoothDevice* remote_device); |
| 49 |
| 50 // Callback called when a new discovery session is started. |
| 51 void OnDiscoverySessionStarted( |
| 52 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); |
| 53 |
| 54 // Callback called when there is an error starting a new discovery session. |
| 55 void OnStartDiscoverySessionError(); |
| 56 |
| 57 // Starts a discovery session for |adapter_|. |
| 58 void StartDiscoverySession(); |
| 59 |
| 60 // Callback called when |discovery_session_| is stopped. |
| 61 void OnDiscoverySessionStopped(); |
| 62 |
| 63 // Callback called when there is an error stopping |discovery_session_|. |
| 64 void OnStopDiscoverySessionError(); |
| 65 |
| 66 // Stops the discovery session given by |discovery_session_|. |
| 67 void StopDiscoverySession(); |
| 68 |
| 69 // Checks if a service with |service_uuid| is offered by |remote_device|. |
| 70 bool HasService(device::BluetoothDevice* remote_device); |
| 71 |
| 72 // Callback called when there is an error creating the connection. |
| 73 void OnCreateConnectionError( |
| 74 device::BluetoothDevice::ConnectErrorCode error_code); |
| 75 |
| 76 // Creates a GATT connection with |remote_device|, |connection_callback_| will |
| 77 // be called once the connection is established. |
| 78 void CreateConnection(device::BluetoothDevice* remote_device); |
| 79 |
| 80 // The uuid of the service it looks for to establish a GattConnection. |
| 81 device::BluetoothUUID remote_service_uuid_; |
| 82 |
| 83 // The Bluetooth adapter over which the Bluetooth connection will be made. |
| 84 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 85 |
| 86 // The discovery session associated to this object. |
| 87 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 88 |
| 89 // Callback called when the connection is established. |
| 90 device::BluetoothDevice::GattConnectionCallback connection_callback_; |
| 91 |
| 92 base::WeakPtrFactory<BluetoothLowEnergyConnectionFinder> weak_ptr_factory_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyConnectionFinder); |
| 95 }; |
| 96 |
| 97 } // namespace proximity_auth |
| 98 |
| 99 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H |
OLD | NEW |