Fixed crash during devices scanning

PiperOrigin-RevId: 522457603
This commit is contained in:
Guogang Li
2023-04-06 15:47:45 -07:00
committed by Copybara-Service
parent fb4d42ad0d
commit 3432c829e1
6 changed files with 74 additions and 170 deletions
@@ -22,7 +22,6 @@
#include <string>
#include <utility>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble.h"
@@ -48,40 +47,33 @@ class BleMedium : public api::BleMedium {
bool StartAdvertising(
const std::string& service_id, const ByteArray& advertisement_bytes,
const std::string& fast_advertisement_service_uuid) override
ABSL_LOCKS_EXCLUDED(mutex_);
const std::string& fast_advertisement_service_uuid) override;
bool StopAdvertising(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAdvertising(const std::string& service_id) override;
// Returns true once the BLE scan has been initiated.
bool StartScanning(const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
DiscoveredPeripheralCallback callback) override;
// Returns true once BLE scanning for service_id is well and truly stopped;
// after this returns, there must be no more invocations of the
// DiscoveredPeripheralCallback passed in to StartScanning() for service_id.
bool StopScanning(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopScanning(const std::string& service_id) override;
// Returns true once BLE socket connection requests to service_id can be
// accepted.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
AcceptedConnectionCallback callback) override;
bool StopAcceptingConnections(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAcceptingConnections(const std::string& service_id) override;
// Connects to a BLE peripheral.
// On success, returns a new BleSocket.
// On error, returns nullptr.
std::unique_ptr<api::BleSocket> Connect(api::BlePeripheral& peripheral,
const std::string& service_id,
CancellationFlag* cancellation_flag)
ABSL_LOCKS_EXCLUDED(mutex_);
CancellationFlag* cancellation_flag);
private:
void PublisherHandler(
@@ -99,7 +91,6 @@ class BleMedium : public api::BleMedium {
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementWatcherStoppedEventArgs args);
mutable absl::Mutex mutex_;
BluetoothAdapter* adapter_;
std::string service_id_;
@@ -107,19 +98,18 @@ class BleMedium : public api::BleMedium {
// Map to protect the pointer for BlePeripheral because
// DiscoveredPeripheralCallback only keeps the pointer to the object
absl::Mutex peripheral_map_mutex_;
absl::flat_hash_map<std::string, std::unique_ptr<BlePeripheral>>
peripheral_map_ ABSL_GUARDED_BY(mutex_);
peripheral_map_ ABSL_GUARDED_BY(peripheral_map_mutex_);
// WinRT objects
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementPublisher publisher_ ABSL_GUARDED_BY(mutex_) =
nullptr;
BluetoothLEAdvertisementPublisher publisher_ = nullptr;
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementWatcher watcher_ ABSL_GUARDED_BY(mutex_) =
nullptr;
BluetoothLEAdvertisementWatcher watcher_ = nullptr;
bool is_publisher_started_ ABSL_GUARDED_BY(mutex_) = false;
bool is_watcher_started_ ABSL_GUARDED_BY(mutex_) = false;
bool is_publisher_started_ = false;
bool is_watcher_started_ = false;
::winrt::event_token publisher_token_;
::winrt::event_token watcher_token_;