bluetooth_gatt_notify_session.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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_notify_session.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/threading/thread_task_runner_handle.h"
  8. #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
  9. namespace device {
  10. BluetoothGattNotifySession::BluetoothGattNotifySession(
  11. base::WeakPtr<BluetoothRemoteGattCharacteristic> characteristic)
  12. : characteristic_(characteristic),
  13. characteristic_id_(characteristic.get() ? characteristic->GetIdentifier()
  14. : std::string()),
  15. active_(true) {}
  16. BluetoothGattNotifySession::~BluetoothGattNotifySession() {
  17. if (active_) {
  18. Stop(base::DoNothing());
  19. }
  20. }
  21. std::string BluetoothGattNotifySession::GetCharacteristicIdentifier() const {
  22. return characteristic_id_;
  23. }
  24. BluetoothRemoteGattCharacteristic*
  25. BluetoothGattNotifySession::GetCharacteristic() const {
  26. return characteristic_.get();
  27. }
  28. bool BluetoothGattNotifySession::IsActive() {
  29. return active_ && characteristic_ != nullptr &&
  30. characteristic_->IsNotifying();
  31. }
  32. void BluetoothGattNotifySession::Stop(base::OnceClosure callback) {
  33. active_ = false;
  34. if (characteristic_ != nullptr) {
  35. characteristic_->StopNotifySession(this, std::move(callback));
  36. } else {
  37. base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
  38. std::move(callback));
  39. }
  40. }
  41. } // namespace device