Rightpoint/RZBluetooth

RZBPeripheral.cancelConnection cancels commands for all peripherals

fluehre opened this issue · 2 comments

Issue

When you call cancelConnection on a peripheral, it first cancels pending commands. However it does this for all peripherals, not just the one for which we are canceling the connection. In my particular case this was making it impossible to disconnect several devices at a time because they would cancel each other's disconnect commands.

Here's the current code in RZBPeripheral.m:

- (void)cancelAllCommands
{
    NSError *error = [NSError errorWithDomain:RZBluetoothErrorDomain
                                         code:RZBluetoothConnectionCancelled
                                     userInfo:@{}];

    for (RZBCommand *command in [self.dispatch commands]) {
        [self.dispatch completeCommand:command
                            withObject:nil error:error];
    }
}

I believe it needs to be changed to the following:

- (void)cancelAllCommands
{
    NSError *error = [NSError errorWithDomain:RZBluetoothErrorDomain
                                         code:RZBluetoothConnectionCancelled
                                     userInfo:@{}];

    RZBUUIDPath *path = RZBUUIDP(self.identifier);
    NSArray *commands = [self.dispatch commandsOfClass:nil matchingUUIDPath:path];

    for (RZBCommand *command in commands) {
        [self.dispatch completeCommand:command
                            withObject:nil error:error];
    }
}

Great catch! Any chance you can pull together a PR? A test case would be great, but this is pretty problematic. Also, what version are you using?

Yeah, I'll try to get a PR with test case together shortly. I'm on 2.0.0-pre.