iOS CoreBluetooth:centralManager:didConnectPeripheral / didFailToConnectPeripheral:没有被调用

我把我的头发从这个问题中解脱出来。 我试图连接到BLE设备 ,无法看到我在下面的代码中做了什么错误。

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _cm = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } + (NSString*)UUIDString:(CFUUIDRef)uuid { CFStringRef string = CFUUIDCreateString(NULL, uuid); return (__bridge_transfer NSString*)string; } - (void)centralManagerDidUpdateState:(CBCentralManager *)central { if (central.state == CBCentralManagerStatePoweredOn) { [self scanForPeripherals]; } } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { // NSLog(@"Received peripheral : \n%@", peripheral); // NSLog(@"Adv data : %@", advertisementData); [peripheral setDelegate:self]; [central connectPeripheral:peripheral options:nil]; [peripheral readRSSI]; } - (int)scanForPeripherals { NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil]; [_cm scanForPeripheralsWithServices:nil options:options]; return 0; } - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"didConnectPeripheral"); } - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"didDisconnectPeripheral"); } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"failed to connect"); } - (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error { NSLog(@"didReadRSSI"); } 

这些设备不是我自己的。 我不知道它的接近UUID,但据我所知,这将不需要通过CoreBluetooth连接的权利?

所有的设备都在didDiscoverPeripheral:被发现,在select器中我试图连接它们。 但是之后什么都没有。

当我打电话给didDiscoverPeripheral:时,我希望与配对密码请求进行didDiscoverPeripheral: 如果是这样,我没有看到任何对话,为什么呢?

从苹果的文件,它清楚地表明,试图连接到设备后,你应该得到一个didConnectPeripheraldidFailToConnectPeripher调用,但我没有得到。

有什么想法吗? 我已经尝试了近一个星期了。 感谢每一个帮助,谢谢。

如果您不以某种方式保留发送到didDiscoverPeripheralperipheral对象,那么一旦这个委托方法退出,您将不会获得连接。

我build议添加一个属性来跟踪发现的外围设备

 @property (strong,nonatomic) NSMutableArray *peripherals; 

viewDidLoadinit

 self.peripherals=[NSMutableArray new]; 

然后在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) } 

这是Swift版本。