From 2ca7d43f1a13a674a9a2b29d32679b0e95ac5252 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 21 Sep 2022 13:51:52 -0700 Subject: [PATCH] Fixed the BLE start/stop issues PiperOrigin-RevId: 475915082 --- .../implementation/windows/ble_medium.cc | 16 ++++++++++++---- .../platform/implementation/windows/ble_medium.h | 2 -- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index 5aa54356..4f6ab6ee 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -77,6 +77,11 @@ using ::winrt::Windows::Devices::Bluetooth::Advertisement:: using ::winrt::Windows::Devices::Bluetooth::Advertisement:: BluetoothLEAdvertisementPublisherStatusChangedEventArgs; +// BluetoothLEAdvertisement +// https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisement?view=winrt-22621 +using ::winrt::Windows::Devices::Bluetooth::Advertisement:: + BluetoothLEAdvertisement; + // Provides data for a Received event on a BluetoothLEAdvertisementWatcher. A // BluetoothLEAdvertisementReceivedEventArgs instance is created when the // Received event occurs on a BluetoothLEAdvertisementWatcher object. @@ -170,21 +175,24 @@ bool BleMedium::StartAdvertising( BluetoothLEAdvertisementDataSection service_data = BluetoothLEAdvertisementDataSection(0x16, data_writer.DetachBuffer()); + BluetoothLEAdvertisement advertisement; + IVector data_sections = - advertisement_.DataSections(); + advertisement.DataSections(); + data_sections.Append(service_data); - advertisement_.DataSections() = data_sections; + advertisement.DataSections() = data_sections; // Use Extended Advertising if Fast Advertisement Service Uuid is empty // string because the long format advertisement will be used if (fast_advertisement_service_uuid.empty()) { - publisher_ = BluetoothLEAdvertisementPublisher(advertisement_); + publisher_ = BluetoothLEAdvertisementPublisher(advertisement); publisher_.UseExtendedAdvertisement(true); } else { // Extended Advertisement not supported, must make sure // advertisement_bytes is less than 27 bytes if (advertisement_bytes.size() <= 27) { - publisher_ = BluetoothLEAdvertisementPublisher(advertisement_); + publisher_ = BluetoothLEAdvertisementPublisher(advertisement); publisher_.UseExtendedAdvertisement(false); } else { // otherwise no-op diff --git a/internal/platform/implementation/windows/ble_medium.h b/internal/platform/implementation/windows/ble_medium.h index 28ee984c..7d57812b 100644 --- a/internal/platform/implementation/windows/ble_medium.h +++ b/internal/platform/implementation/windows/ble_medium.h @@ -109,8 +109,6 @@ class BleMedium : public api::BleMedium { BluetoothLEAdvertisementPublisher publisher_ = nullptr; ::winrt::Windows::Devices::Bluetooth::Advertisement:: BluetoothLEAdvertisementWatcher watcher_ = nullptr; - ::winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisement - advertisement_; bool is_publisher_started_ = false; bool is_watcher_started_ = false;