Fixes a potential crash when shutting down a BleMedium.

PiperOrigin-RevId: 792365629
This commit is contained in:
Edwin Wu
2025-08-07 17:08:04 -07:00
committed by Copybara-Service
parent 0847245618
commit e95b44cf61
5 changed files with 27 additions and 3 deletions
@@ -238,6 +238,11 @@ typedef void (^GNCGATTConnectionCompletionHandler)(GNCBLEGATTClient *_Nullable c
peripheral:(id<GNCPeripheral>)remotePeripheral
completionHandler:(nullable GNCOpenL2CAPStreamCompletionHandler)completionHandler;
/**
* Stops all BLE operations.
*/
- (void)stop;
@end
NS_ASSUME_NONNULL_END
@@ -34,6 +34,7 @@ NS_ASSUME_NONNULL_BEGIN
static char *const kBLEMediumQueueLabel = "com.nearby.GNCBLEMedium";
// TODO: b/762454867 - Make this a flag.
static const NSTimeInterval kPeripheralConnectTimeout = 10.0; // 10 seconds timeout
static const NSTimeInterval kStopTimeout = 5.0;
static NSError *AlreadyScanningError() {
return [NSError errorWithDomain:GNCBLEErrorDomain code:GNCBLEErrorAlreadyScanning userInfo:nil];
@@ -122,8 +123,17 @@ static GNCBLEL2CAPServer *_Nonnull CreateL2CapServer(
return self;
}
- (void)dealloc {
[self cancelConnectionTimeout];
- (void)stop {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(_queue, ^{
[self cancelConnectionTimeout];
dispatch_semaphore_signal(semaphore);
});
dispatch_time_t timeout =
dispatch_time(DISPATCH_TIME_NOW, kStopTimeout * NSEC_PER_SEC); // 5 seconds timeout
if (dispatch_semaphore_wait(semaphore, timeout) != 0) {
GNCLoggerError(@"GNCBLEMedium stop timeout.");
}
}
- (BOOL)supportsExtendedAdvertisements {
@@ -80,4 +80,10 @@ void GNCEnsureFileAtPath(std::string path) {
XCTAssertEqualObjects(@(actual.c_str()), expected);
}
- (void)testCreateBleV2Medium {
auto bluetooth_adapter = nearby::api::ImplementationPlatform::CreateBluetoothAdapter();
auto ble_medium = nearby::api::ImplementationPlatform::CreateBleV2Medium(*bluetooth_adapter);
XCTAssertNotEqual(ble_medium.get(), nullptr);
}
@end
@@ -47,7 +47,7 @@ namespace apple {
class BleMedium : public api::ble_v2::BleMedium {
public:
BleMedium();
~BleMedium() override = default;
~BleMedium() override;
// Async interface for StartAdvertising.
//
@@ -82,6 +82,9 @@ NSString *ConvertDataToHexString(NSData *data) {
BleMedium::BleMedium() : medium_([[GNCBLEMedium alloc] init]) {}
// ble_medium.mm
BleMedium::~BleMedium() { [medium_ stop]; }
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession> BleMedium::StartAdvertising(
const api::ble_v2::BleAdvertisementData &advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters,