Rightpoint / RZBluetooth

Core Bluetooth helper library
Other
136 stars 47 forks source link

RZBPeripheral.cancelConnection cancels commands for all peripherals #99

Closed fluehre closed 5 years ago

fluehre commented 5 years ago

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];
    }
}
KingOfBrian commented 5 years ago

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?

fluehre commented 5 years ago

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