Skip to content

Commit

Permalink
[MacOS] Add timeout to connect (#258)
Browse files Browse the repository at this point in the history
* [MacOS] Add timeout to connect

* Made my suggested changes.

---------

Co-authored-by: Bliss Chapman <[email protected]>
Co-authored-by: Kevin Dewald <[email protected]>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 8e08edb commit 311386e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion simpleble/src/backends/macos/PeripheralBaseMacOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
} \
} while (0)

#define WAIT_UNTIL_FALSE_WITH_TIMEOUT(obj, var, timeout) \
do { \
BOOL _tmpVar = YES; \
NSDate* endDate = [NSDate dateWithTimeInterval:timeout sinceDate:NSDate.now]; \
while (_tmpVar && [NSDate.now compare:endDate] == NSOrderedAscending) { \
[NSThread sleepForTimeInterval:0.01]; \
@synchronized(obj) { \
_tmpVar = (var); \
} \
} \
} while (0)

// --------------------------------------------------

@interface BleTask : NSObject
Expand Down Expand Up @@ -134,7 +146,7 @@ - (void)connect {
[self.centralManager connectPeripheral:self.peripheral options:@{}]; // TODO: Do we need to pass any options?
}

WAIT_UNTIL_FALSE(self, _task.pending);
WAIT_UNTIL_FALSE_WITH_TIMEOUT(self, _task.pending, 5.0);

if (self.peripheral.state != CBPeripheralStateConnected || _task.error != nil) {
[self throwBasedOnError:_task.error withFormat:@"Peripheral Connection"];
Expand Down

0 comments on commit 311386e

Please sign in to comment.