Fixed crash during devices scanning

PiperOrigin-RevId: 518127975
This commit is contained in:
Guogang Li
2023-03-20 18:03:49 -07:00
committed by Copybara-Service
parent d0b258971d
commit 78145f4433
6 changed files with 164 additions and 70 deletions
@@ -22,6 +22,7 @@
#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"
@@ -47,33 +48,40 @@ class BleMedium : public api::BleMedium {
bool StartAdvertising(
const std::string& service_id, const ByteArray& advertisement_bytes,
const std::string& fast_advertisement_service_uuid) override;
const std::string& fast_advertisement_service_uuid) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAdvertising(const std::string& service_id) override;
bool StopAdvertising(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// 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;
DiscoveredPeripheralCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
// 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;
bool StopScanning(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true once BLE socket connection requests to service_id can be
// accepted.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback) override;
AcceptedConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAcceptingConnections(const std::string& service_id) override;
bool StopAcceptingConnections(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// 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);
CancellationFlag* cancellation_flag)
ABSL_LOCKS_EXCLUDED(mutex_);
private:
void PublisherHandler(
@@ -91,6 +99,7 @@ class BleMedium : public api::BleMedium {
::winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementWatcherStoppedEventArgs args);
mutable absl::Mutex mutex_;
BluetoothAdapter* adapter_;
std::string service_id_;
@@ -98,9 +107,8 @@ 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(peripheral_map_mutex_);
peripheral_map_ ABSL_GUARDED_BY(mutex_);
// WinRT objects
::winrt::Windows::Devices::Bluetooth::Advertisement::