@@ -61,7 +61,11 @@ @implementation MTRCommissionableBrowserResult
61
61
#endif // CONFIG_NETWORK_LAYER_BLE
62
62
{
63
63
public:
64
- CHIP_ERROR Start (id <MTRCommissionableBrowserDelegate> delegate, MTRDeviceController * controller, dispatch_queue_t queue)
64
+ #if CONFIG_NETWORK_LAYER_BLE
65
+ id mBleScannerDelegateOwner ;
66
+ #endif // CONFIG_NETWORK_LAYER_BLE
67
+
68
+ CHIP_ERROR Start (id owner, id <MTRCommissionableBrowserDelegate> delegate, MTRDeviceController * controller, dispatch_queue_t queue)
65
69
{
66
70
assertChipStackLockedByCurrentThread ();
67
71
@@ -77,7 +81,10 @@ CHIP_ERROR Start(id<MTRCommissionableBrowserDelegate> delegate, MTRDeviceControl
77
81
ResetCounters ();
78
82
79
83
#if CONFIG_NETWORK_LAYER_BLE
84
+ mBleScannerDelegateOwner = owner; // retain the owner until OnBleScanStopped is called (see rdar://128723029)
80
85
ReturnErrorOnFailure (PlatformMgrImpl ().StartBleScan (this ));
86
+ #else
87
+ (void )owner;
81
88
#endif // CONFIG_NETWORK_LAYER_BLE
82
89
83
90
ReturnErrorOnFailure (Resolver::Instance ().Init (chip::DeviceLayer::UDPEndPointManager ()));
@@ -281,6 +288,7 @@ void OnBrowseStop(CHIP_ERROR error) override
281
288
void OnBleScanAdd (BLE_CONNECTION_OBJECT connObj, const ChipBLEDeviceIdentificationInfo & info) override
282
289
{
283
290
assertChipStackLockedByCurrentThread ();
291
+ VerifyOrReturn (mDelegate != nil );
284
292
285
293
auto result = [[MTRCommissionableBrowserResult alloc ] init ];
286
294
result.instanceName = [NSString stringWithUTF8String: kBleKey ];
@@ -303,6 +311,7 @@ void OnBleScanAdd(BLE_CONNECTION_OBJECT connObj, const ChipBLEDeviceIdentificati
303
311
void OnBleScanRemove (BLE_CONNECTION_OBJECT connObj) override
304
312
{
305
313
assertChipStackLockedByCurrentThread ();
314
+ VerifyOrReturn (mDelegate != nil );
306
315
307
316
auto key = [NSString stringWithFormat: @" %@ " , connObj];
308
317
if ([mDiscoveredResults objectForKey: key] == nil ) {
@@ -319,6 +328,12 @@ void OnBleScanRemove(BLE_CONNECTION_OBJECT connObj) override
319
328
[mDelegate controller: mController didFindCommissionableDevice: result];
320
329
});
321
330
}
331
+
332
+ void OnBleScanStopped () override
333
+ {
334
+ mBleScannerDelegateOwner = nil ;
335
+ }
336
+
322
337
#endif // CONFIG_NETWORK_LAYER_BLE
323
338
324
339
private:
@@ -354,7 +369,7 @@ - (instancetype)initWithDelegate:(id<MTRCommissionableBrowserDelegate>)delegate
354
369
355
370
- (BOOL )start
356
371
{
357
- VerifyOrReturnValue (CHIP_NO_ERROR == _browser.Start (_delegate, _controller, _queue), NO );
372
+ VerifyOrReturnValue (CHIP_NO_ERROR == _browser.Start (self, _delegate, _controller, _queue), NO );
358
373
return YES ;
359
374
}
360
375
0 commit comments