Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MacOS] Add timeout to connect #258

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading