iOS CoreBluetooth : centralManager:didConnectPeripheral / didFailToConnectPeripheral: not getting called

前端 未结 2 1312
北海茫月
北海茫月 2020-12-16 11:24

I\'m pulling my hair out of this problems. I\'m trying to connect to BLE devices, can\'t see what I\'ve done wrong in my code below.

- (void         


        
2条回答
  •  [愿得一人]
    2020-12-16 11:32

    If you don't somehow retain the peripheral object that is delivered to didDiscoverPeripheral then it is released once this delegate method exits and you won't get a connection.

    I suggest adding a property to track discovered peripherals

    @property (strong,nonatomic) NSMutableArray *peripherals;
    

    initialise this in viewDidLoad or init

    self.peripherals=[NSMutableArray new];
    

    And then add the peripheral to it in didDiscoverPeripheral

    -(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    {
        NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
        [self.peripherals addObject:peripheral];
        [central connectPeripheral:peripheral options:nil];
    }
    

提交回复
热议问题