bluetooth_gatt_connection.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "device/bluetooth/bluetooth_gatt_connection.h"
  5. #include "device/bluetooth/bluetooth_adapter.h"
  6. namespace device {
  7. BluetoothGattConnection::BluetoothGattConnection(
  8. scoped_refptr<device::BluetoothAdapter> adapter,
  9. const std::string& device_address)
  10. : adapter_(adapter), device_address_(device_address) {
  11. DCHECK(adapter_.get());
  12. DCHECK(!device_address_.empty());
  13. device_ = adapter_->GetDevice(device_address_);
  14. DCHECK(device_);
  15. owns_reference_for_connection_ = true;
  16. device_->AddGattConnection(this);
  17. }
  18. BluetoothGattConnection::~BluetoothGattConnection() {
  19. Disconnect();
  20. }
  21. const std::string& BluetoothGattConnection::GetDeviceAddress() const {
  22. return device_address_;
  23. }
  24. bool BluetoothGattConnection::IsConnected() {
  25. if (!owns_reference_for_connection_)
  26. return false;
  27. DCHECK(adapter_->GetDevice(device_address_));
  28. DCHECK(device_->IsGattConnected());
  29. return true;
  30. }
  31. void BluetoothGattConnection::Disconnect() {
  32. if (!owns_reference_for_connection_)
  33. return;
  34. owns_reference_for_connection_ = false;
  35. device_->RemoveGattConnection(this);
  36. }
  37. void BluetoothGattConnection::InvalidateConnectionReference() {
  38. owns_reference_for_connection_ = false;
  39. }
  40. } // namespace device