7

I know this is a stupid question but here goes.

I have an older app that uses isConnected. Now I get a warning that it is deprecated. Can I just delete this line of code without any ramification or how do I handle this. Sorry for being so dense.

here is some code it is from the CBPeripheral framework.

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    // Deal with errors (if any)
    if (error) {
        NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
        [self cleanup];
        return;
    }
}
- (void)cleanup
{
    // Don't do anything if we're not connected
    if (!self.discoveredPeripheral.isConnected) // here is where the warning comes {
        return;
    }

I think I found the answer should be

- (void)cleanup
    {
        // Don't do anything if we're not connected
        if (CBPeripheralStateDisconnected)  {
            return;
        }

I also added @property(readonly) CBPeripheralState state; in my .h

I don't get an error Can anyone verify this for me?

user1114881
  • 731
  • 1
  • 12
  • 25
  • What class is isConnected on? When you look up isConnected for that class in the documentation, you should be told what this is deprecated in favor of. – Brad Larson Jun 26 '14 at 18:14
  • Please show some code, is this property from `CBPeripheral` or something other? This question will be probably marked as very low quality.. – Tomasz Szulc Jun 26 '14 at 18:14
  • That's what I do each time I have a warning, I delete the code and the problem is gone. But often, by doing this, something else goes wrong. More seriously, more code needed. – Vincent Guerci Jun 26 '14 at 18:37

1 Answers1

17

As Apple Documentation says:

isConnected

A Boolean value indicating whether the peripheral is currently connected to the central manager. (read-only) (Deprecated in iOS 7.0. Use the state property instead.)

Just replace your code by:

if (self.discoveredPeripheral.state != CBPeripheralStateConnected)
    return;

On the other hand, if that's all you have in this method, basically you are doing nothing. So you could just remove that dead code. Which makes me thinks something missing... No cleanup?

Vincent Guerci
  • 14,379
  • 4
  • 50
  • 56
  • 1
    I get a warning for comparison of constant – Michal Shatz May 05 '15 at 17:49
  • @MichalShatz you can just use `if (!self.discoveredPeripheral.state)`. Docs: [CBPeripheralState](https://developer.apple.com/library/prerelease/ios/documentation/CoreBluetooth/Reference/CBPeripheral_Class/index.html#//apple_ref/c/tdef/CBPeripheralState). – Daniel Storm Mar 03 '16 at 20:38