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

前端 未结 2 1299
北海茫月
北海茫月 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];
    }
    
    0 讨论(0)
  • 2020-12-16 11:34
    var peripherals = [CBPeripheral]()
    
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        peripherals.append(peripheral)
        bleManager.connectPeripheral(peripheral, options: nil)
    }
    

    This is the Swift version.

    0 讨论(0)
提交回复
热议问题