bluetooth_gatt_connection.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2014 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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "device/bluetooth/bluetooth_export.h"
  10. namespace device {
  11. class BluetoothAdapter;
  12. class BluetoothDevice;
  13. // BluetoothGattConnection represents a GATT connection to a Bluetooth device
  14. // that has GATT services. Instances are obtained from a BluetoothDevice,
  15. // and the connection is kept alive as long as there is at least one
  16. // active BluetoothGattConnection object. BluetoothGattConnection objects
  17. // automatically update themselves, when the connection is terminated by the
  18. // operating system (e.g. due to user action).
  19. class DEVICE_BLUETOOTH_EXPORT BluetoothGattConnection {
  20. public:
  21. BluetoothGattConnection(scoped_refptr<device::BluetoothAdapter> adapter,
  22. const std::string& device_address);
  23. BluetoothGattConnection(const BluetoothGattConnection&) = delete;
  24. BluetoothGattConnection& operator=(const BluetoothGattConnection&) = delete;
  25. // Destructor automatically closes this GATT connection. If this is the last
  26. // remaining GATT connection and this results in a call to the OS, that call
  27. // may not always succeed. Users can make an explicit call to
  28. // BluetoothGattConnection::Close to make sure that they are notified of
  29. // a possible error via the callback.
  30. virtual ~BluetoothGattConnection();
  31. // Returns the Bluetooth address of the device that this connection is open
  32. // to.
  33. const std::string& GetDeviceAddress() const;
  34. // Returns true if this GATT connection is open.
  35. virtual bool IsConnected();
  36. // Disconnects this GATT connection. The device may still remain connected due
  37. // to other GATT connections. When all BluetoothGattConnection objects are
  38. // disconnected the BluetoothDevice object will disconnect GATT.
  39. virtual void Disconnect();
  40. protected:
  41. friend BluetoothDevice; // For InvalidateConnectionReference.
  42. // Sets this object to no longer have a reference maintaining the connection.
  43. // Only to be called by BluetoothDevice to avoid reentrant code to
  44. // RemoveGattConnection in that destructor after BluetoothDevice subclasses
  45. // have already been destroyed.
  46. void InvalidateConnectionReference();
  47. // The Bluetooth adapter that this connection is associated with. A reference
  48. // is held because BluetoothGattConnection keeps the connection alive.
  49. scoped_refptr<BluetoothAdapter> adapter_;
  50. // Bluetooth address of the underlying device.
  51. std::string device_address_;
  52. raw_ptr<BluetoothDevice> device_ = nullptr;
  53. private:
  54. bool owns_reference_for_connection_ = false;
  55. };
  56. } // namespace device
  57. #endif // DEVICE_BLUETOOTH_BLUETOOTH_GATT_CONNECTION_H_