mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fixed discovery issue in BLE V2
PiperOrigin-RevId: 536557212
This commit is contained in:
committed by
Copybara-Service
parent
1e868a1a65
commit
72f74f3698
@@ -209,6 +209,11 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
service_uuid_ = service_uuid;
|
||||
tx_power_level_ = tx_power_level;
|
||||
scan_callback_ = std::move(callback);
|
||||
{
|
||||
absl::MutexLock lock(&peripheral_map_mutex_);
|
||||
peripheral_map_.clear();
|
||||
mac_address_to_peripheral_id_map_.clear();
|
||||
}
|
||||
|
||||
watcher_ = BluetoothLEAdvertisementWatcher();
|
||||
watcher_token_ = watcher_.Stopped({this, &BleV2Medium::WatcherHandler});
|
||||
@@ -922,13 +927,19 @@ void BleV2Medium::AdvertisementReceivedHandler(
|
||||
BleV2Peripheral* peripheral_ptr = nullptr;
|
||||
{
|
||||
absl::MutexLock lock(&peripheral_map_mutex_);
|
||||
if (!peripheral_map_.contains(mac_address_string)) {
|
||||
peripheral_map_[mac_address_string] = std::move(peripheral);
|
||||
if (mac_address_to_peripheral_id_map_.contains(mac_address_string)) {
|
||||
peripheral_map_[mac_address_to_peripheral_id_map_[mac_address_string]]
|
||||
->SetAddress(
|
||||
uint64_to_mac_address_string(args.BluetoothAddress()));
|
||||
} else {
|
||||
peripheral_map_[mac_address_string]->SetAddress(
|
||||
uint64_to_mac_address_string(args.BluetoothAddress()));
|
||||
mac_address_to_peripheral_id_map_[mac_address_string] =
|
||||
peripheral->GetUniqueId();
|
||||
peripheral_map_[peripheral->GetUniqueId()] = std::move(peripheral);
|
||||
}
|
||||
peripheral_ptr = peripheral_map_[mac_address_string].get();
|
||||
peripheral_ptr =
|
||||
peripheral_map_
|
||||
[mac_address_to_peripheral_id_map_[mac_address_string]]
|
||||
.get();
|
||||
}
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << "New BLE peripheral: " << peripheral_ptr
|
||||
@@ -1051,27 +1062,24 @@ void BleV2Medium::AdvertisementFoundHandler(
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
for (auto& item : peripherals_) {
|
||||
absl::MutexLock lock(&peripheral_map_mutex_);
|
||||
for (auto& item : peripheral_map_) {
|
||||
if (item.second->GetAddress() == mac_address) {
|
||||
NEARBY_LOGS(WARNING) << __func__ << ": No matched peripheral device.";
|
||||
callback(*(item.second));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
auto peripheral = std::make_unique<BleV2Peripheral>();
|
||||
peripheral->SetAddress(mac_address);
|
||||
if (peripheral->GetUniqueId() == 0) {
|
||||
return false;
|
||||
}
|
||||
BleV2Peripheral* ptr = peripheral.get();
|
||||
peripherals_[peripheral->GetUniqueId()] = std::move(peripheral);
|
||||
callback(*ptr);
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
auto it = peripherals_.find(id);
|
||||
if (it == peripherals_.end()) {
|
||||
absl::MutexLock lock(&peripheral_map_mutex_);
|
||||
auto it = peripheral_map_.find(id);
|
||||
if (it == peripheral_map_.end()) {
|
||||
NEARBY_LOGS(WARNING) << __func__ << ": No matched peripheral device.";
|
||||
return false;
|
||||
}
|
||||
callback(*(it->second));
|
||||
|
||||
@@ -118,12 +118,6 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::TxPowerLevel tx_power_level_;
|
||||
ScanCallback scan_callback_;
|
||||
|
||||
// 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<BleV2Peripheral>>
|
||||
peripheral_map_ ABSL_GUARDED_BY(peripheral_map_mutex_);
|
||||
|
||||
absl::Mutex map_mutex_;
|
||||
// std::map<std::string, std::unique_ptr<BleV2Peripheral>>
|
||||
absl::flat_hash_map<std::string, std::unique_ptr<BleV2Peripheral>>
|
||||
@@ -148,9 +142,14 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
|
||||
BleGattServer* ble_gatt_server_ = nullptr;
|
||||
|
||||
// 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<BleV2Peripheral::UniqueId,
|
||||
std::unique_ptr<BleV2Peripheral>>
|
||||
peripherals_;
|
||||
peripheral_map_ ABSL_GUARDED_BY(peripheral_map_mutex_);
|
||||
absl::flat_hash_map<std::string, BleV2Peripheral::UniqueId>
|
||||
mac_address_to_peripheral_id_map_ ABSL_GUARDED_BY(peripheral_map_mutex_);
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
|
||||
Reference in New Issue
Block a user