From 4045af569fe95ec6b124e92bb5c0e62fe37e7e57 Mon Sep 17 00:00:00 2001 From: aaronyujiaze Date: Fri, 3 Jun 2022 11:04:27 -0700 Subject: [PATCH] Add Extended Advertising support for BlePeripheral discovery PiperOrigin-RevId: 452801874 --- .../implementation/windows/ble_medium.cc | 26 ++++++++++++++----- .../implementation/windows/ble_medium.h | 3 ++- .../windows/bluetooth_adapter.cc | 14 +++++++++- .../windows/bluetooth_adapter.h | 9 +++++-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index eb818e1b..29e8eaae 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -21,7 +21,7 @@ #include "absl/strings/escaping.h" #include "absl/synchronization/mutex.h" -#include "internal/platform/implementation/ble.h" +#include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" #include "winrt/Windows.Devices.Bluetooth.Advertisement.h" @@ -117,7 +117,7 @@ constexpr uint16_t kCopresenceServiceUuid = 0xf3fe; } // namespace BleMedium::BleMedium(api::BluetoothAdapter& adapter) - : adapter_(static_cast(&adapter)) {} + : adapter_(static_cast(&adapter)) {} bool BleMedium::StartAdvertising( const std::string& service_id, const ByteArray& advertisement_bytes, @@ -233,6 +233,9 @@ bool BleMedium::StartScanning( std::future watcher_state_future = watcher_started_promise_.get_future(); + if (adapter_->IsExtendedAdvertisingSupported()) { + watcher_.AllowExtendedAdvertisements(true); + } // Active mode indicates that scan request packets will be sent to query for // Scan Response watcher_.ScanningMode(BluetoothLEScanningMode::Active); @@ -465,10 +468,21 @@ void BleMedium::AdvertisementReceivedHandler( peripheral->SetName(peripheral_name); peripheral->SetAdvertisementBytes(advertisement_data); - NEARBY_LOGS(INFO) << "Sending Fast Advertisement packet for processing."; - advertisement_received_callback_.peripheral_discovered_cb( - /*ble_peripheral*/ *(peripheral.get()), /*service_id*/ service_id_, - /*is_fast_advertisement*/ true); + // Received Fast Advertisement packet + if (unconsumed_buffer_length <= 27) { + NEARBY_LOGS(INFO) + << "Sending Fast Advertisement packet for processing."; + advertisement_received_callback_.peripheral_discovered_cb( + /*ble_peripheral*/ *(peripheral.get()), /*service_id*/ service_id_, + /*is_fast_advertisement*/ true); + } else { + // Received Extended Advertising packet + NEARBY_LOGS(INFO) + << "Sending Extended Advertising packet for processing."; + advertisement_received_callback_.peripheral_discovered_cb( + /*ble_peripheral*/ *(peripheral.get()), /*service_id*/ service_id_, + /*is_fast_advertisement*/ false); + } } } } diff --git a/internal/platform/implementation/windows/ble_medium.h b/internal/platform/implementation/windows/ble_medium.h index 19ddc55f..4f5ea9c2 100644 --- a/internal/platform/implementation/windows/ble_medium.h +++ b/internal/platform/implementation/windows/ble_medium.h @@ -25,6 +25,7 @@ #include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/windows/ble.h" +#include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "winrt/Windows.Devices.Bluetooth.Advertisement.h" namespace location { @@ -85,7 +86,7 @@ class BleMedium : public api::BleMedium { enum class WatcherState { kStarted = 0, kStopped, kError }; absl::Mutex mutex_; - api::BluetoothAdapter* adapter_; + BluetoothAdapter* adapter_; ByteArray advertisement_byte_ ABSL_GUARDED_BY(mutex_); std::string service_id_; diff --git a/internal/platform/implementation/windows/bluetooth_adapter.cc b/internal/platform/implementation/windows/bluetooth_adapter.cc index ab561817..9c39bc03 100644 --- a/internal/platform/implementation/windows/bluetooth_adapter.cc +++ b/internal/platform/implementation/windows/bluetooth_adapter.cc @@ -47,7 +47,7 @@ namespace location { namespace nearby { namespace windows { -BluetoothAdapter::BluetoothAdapter() { +BluetoothAdapter::BluetoothAdapter() : windows_bluetooth_adapter_(nullptr) { windows_bluetooth_adapter_ = winrt::Windows::Devices::Bluetooth::BluetoothAdapter::GetDefaultAsync() .get(); @@ -90,6 +90,18 @@ bool BluetoothAdapter::IsEnabled() const { return windows_bluetooth_radio_.State() == RadioState::On; } +// Returns true if the Bluetooth hardware supports Bluetooth 5.0 Extended +// Advertising +bool BluetoothAdapter::IsExtendedAdvertisingSupported() const { + if (windows_bluetooth_adapter_ == nullptr) { + NEARBY_LOGS(ERROR) << __func__ << ": No Bluetooth adapter on this device."; + return false; + } + // Indicates whether the adapter supports the 5.0 Extended Advertising format. + // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter.isextendedadvertisingsupported?view=winrt-22621 + return windows_bluetooth_adapter_.IsExtendedAdvertisingSupported(); +} + // https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode() // // Returns ScanMode::kUnknown on error. diff --git a/internal/platform/implementation/windows/bluetooth_adapter.h b/internal/platform/implementation/windows/bluetooth_adapter.h index f4bb7954..6f786a95 100644 --- a/internal/platform/implementation/windows/bluetooth_adapter.h +++ b/internal/platform/implementation/windows/bluetooth_adapter.h @@ -30,7 +30,8 @@ namespace windows { // Represents a Bluetooth adapter. // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter?view=winrt-20348 -using winrt::Windows::Devices::Bluetooth::IBluetoothAdapter; +using WindowsBluetoothAdapter = + winrt::Windows::Devices::Bluetooth::BluetoothAdapter; // Represents a radio device on the system. // https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radio?view=winrt-20348 @@ -81,9 +82,13 @@ class BluetoothAdapter : public api::BluetoothAdapter { } } + // Returns true if the Bluetooth hardware supports Bluetooth 5.0 Extended + // Advertising + bool IsExtendedAdvertisingSupported() const; + private: void process_error(); - IBluetoothAdapter windows_bluetooth_adapter_; + WindowsBluetoothAdapter windows_bluetooth_adapter_; IRadio windows_bluetooth_radio_; char *GetGenericBluetoothAdapterInstanceID(void) const;