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

做~自己de王妃 提交于 2019-11-29 03:01:47

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];
}
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!