Add Extended Advertising support for BlePeripheral discovery

PiperOrigin-RevId: 452801874
This commit is contained in:
aaronyujiaze
2022-06-03 11:06:43 -07:00
committed by Copybara-Service
parent 45386948be
commit 4045af569f
4 changed files with 42 additions and 10 deletions
@@ -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<api::BluetoothAdapter*>(&adapter)) {}
: adapter_(static_cast<BluetoothAdapter*>(&adapter)) {}
bool BleMedium::StartAdvertising(
const std::string& service_id, const ByteArray& advertisement_bytes,
@@ -233,6 +233,9 @@ bool BleMedium::StartScanning(
std::future<WatcherState> 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);
}
}
}
}
@@ -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_;
@@ -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.
@@ -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;