Skip to content

Commit

Permalink
[linux] Remove known devices before running BLE scan (#5946)
Browse files Browse the repository at this point in the history
* [python] Remove known devices before running BLE scan

Linux BLE layer tries to connect to known devices, cached
by Bluez daemon before running the active scan. It may cause
an issue when trying to connect to a device which is no
longer advertising or it has changed its BLE address which
can be especially painful after enabling features like BLE
address randomization.

Remove known devices before running an active BLE scan.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
Damian-Nordic and restyled-commits authored Apr 12, 2021
1 parent dada1be commit 4d7fd4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/platform/Linux/bluez/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,35 @@ void ChipDeviceScanner::ReportDevice(BluezDevice1 * device)
mDelegate->OnDeviceScanned(device, deviceInfo);
}

void ChipDeviceScanner::RemoveDevice(BluezDevice1 * device)
{
if (device == nullptr)
{
return;
}

if (strcmp(bluez_device1_get_adapter(device), g_dbus_proxy_get_object_path(G_DBUS_PROXY(mAdapter))) != 0)
{
return;
}

chip::Ble::ChipBLEDeviceIdentificationInfo deviceInfo;

if (!BluezGetChipDeviceInfo(*device, deviceInfo))
{
return;
}

const auto devicePath = g_dbus_proxy_get_object_path(G_DBUS_PROXY(device));
GError * error = nullptr;

if (!bluez_adapter1_call_remove_device_sync(mAdapter, devicePath, nullptr, &error))
{
ChipLogDetail(Ble, "Failed to remove device %s: %s", devicePath, error->message);
g_error_free(error);
}
}

int ChipDeviceScanner::MainLoopStartScan(ChipDeviceScanner * self)
{
GError * error = nullptr;
Expand All @@ -238,10 +267,10 @@ int ChipDeviceScanner::MainLoopStartScan(ChipDeviceScanner * self)
self->mInterfaceChangedSignal =
g_signal_connect(self->mManager, "interface-proxy-properties-changed", G_CALLBACK(SignalInterfaceChanged), self);

ChipLogProgress(Ble, "BLE scanning through known devices.");
ChipLogProgress(Ble, "BLE removing known devices.");
for (BluezObject & object : BluezObjectList(self->mManager))
{
self->ReportDevice(bluez_object_get_device1(&object));
self->RemoveDevice(bluez_object_get_device1(&object));
}

ChipLogProgress(Ble, "BLE initiating scan.");
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Linux/bluez/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class ChipDeviceScanner
/// Check if a given device is a CHIP device and if yes, report it as discovered
void ReportDevice(BluezDevice1 * device);

/// Check if a given device is a CHIP device and if yes, remove it from the adapter
/// so that it can be re-discovered if it's still advertising.
void RemoveDevice(BluezDevice1 * device);

GDBusObjectManager * mManager = nullptr;
BluezAdapter1 * mAdapter = nullptr;
GCancellable * mCancellable = nullptr;
Expand Down

0 comments on commit 4d7fd4d

Please sign in to comment.