Fixed crash when stopping advertising

PiperOrigin-RevId: 542580849
This commit is contained in:
Guogang Li
2023-06-22 09:23:00 -07:00
committed by Copybara-Service
parent 3cf20ee3bc
commit bd30b11401
2 changed files with 22 additions and 11 deletions
@@ -245,7 +245,12 @@ bool BleMedium::StopAdvertising(const std::string& service_id) {
return false;
}
publisher_.Stop();
// publisher_ may be null when status changed during advertising.
if (publisher_ != nullptr &&
publisher_.Status() ==
BluetoothLEAdvertisementPublisherStatus::Started) {
publisher_.Stop();
}
// Don't need to wait for the status becomes to `Stopped`. If application
// starts to scanning immediately, the scanning still needs to wait the
@@ -305,8 +310,8 @@ bool BleMedium::StartScanning(
if (adapter_->IsExtendedAdvertisingSupported()) {
watcher_.AllowExtendedAdvertisements(true);
}
// Active mode indicates that scan request packets will be sent to query for
// Scan Response
// Active mode indicates that scan request packets will be sent to query
// for Scan Response
watcher_.ScanningMode(BluetoothLEScanningMode::Active);
::winrt::Windows::Devices::Bluetooth::BluetoothSignalStrengthFilter filter;
filter.SamplingInterval(TimeSpan(std::chrono::seconds(2)));
@@ -450,8 +455,8 @@ void BleMedium::PublisherHandler(
"radio not available.";
break;
case BluetoothError::ResourceInUse:
NEARBY_LOGS(ERROR)
<< "Nearby BLE Medium advertising failed due to resource in use.";
NEARBY_LOGS(ERROR) << "Nearby BLE Medium advertising failed due to "
"resource in use.";
break;
case BluetoothError::DeviceNotConnected:
NEARBY_LOGS(ERROR) << "Nearby BLE Medium advertising failed due to "
@@ -479,8 +484,8 @@ void BleMedium::PublisherHandler(
break;
case BluetoothError::OtherError:
default:
NEARBY_LOGS(ERROR)
<< "Nearby BLE Medium advertising failed due to unknown errors.";
NEARBY_LOGS(ERROR) << "Nearby BLE Medium advertising failed due to "
"unknown errors.";
break;
}
break;
@@ -527,8 +532,8 @@ void BleMedium::WatcherHandler(
<< "Nearby BLE Medium stoped to scan due to disabled by user.";
break;
case BluetoothError::NotSupported:
NEARBY_LOGS(ERROR)
<< "Nearby BLE Medium stoped to scan due to hardware not supported.";
NEARBY_LOGS(ERROR) << "Nearby BLE Medium stoped to scan due to "
"hardware not supported.";
break;
case BluetoothError::TransportNotSupported:
NEARBY_LOGS(ERROR) << "Nearby BLE Medium stoped to scan due to "
@@ -605,7 +610,8 @@ void BleMedium::AdvertisementReceivedHandler(
service_id_) != advertisement_data) {
NEARBY_LOGS(INFO) << "BLE reports lost device: " << peripheral_name;
// Lost the device first and then the report discovered the device.
// Lost the device first and then the report discovered the
// device.
advertisement_received_callback_.peripheral_lost_cb(
/*ble_peripheral*/ *peripheral_map_[peripheral_name],
/*service_id*/ service_id_);
@@ -661,7 +661,12 @@ bool BleV2Medium::StopBleAdvertising() {
return false;
}
publisher_.Stop();
// publisher_ may be null when status changed during advertising.
if (publisher_ != nullptr &&
publisher_.Status() ==
BluetoothLEAdvertisementPublisherStatus::Started) {
publisher_.Stop();
}
// Don't need to wait for the status becomes to `Stopped`. If application
// starts to scanning immediately, the scanning still needs to wait the