11

I want use CoreBluetooth.framework in IOS8 to achieve data transfer, i did discover peripheral in the follow method and try connect the peripheral.

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

But it did not call the delegate methods didFailToConnectPeripheral or didConnectPeripheral , I waner what's wrong with it, is the code error or IOS8 need some extra things ? How to deal it, thank in advance!

here is my code in github ,I write a Program to be server and another be Central.

Clavis
  • 115
  • 1
  • 5
  • 11
    Did u tried storing the peripheral object in an ivar and then connect to it? ie `self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];` – Sj. Jan 27 '15 at 10:15
  • 1
    possible duplicate of [iOS CoreBluetooth : centralManager:didConnectPeripheral / didFailToConnectPeripheral: not getting called](http://stackoverflow.com/questions/26377470/ios-corebluetooth-centralmanagerdidconnectperipheral-didfailtoconnectperiph) – Paulw11 Jan 27 '15 at 20:18
  • It work, thank you so much! – Clavis Jan 28 '15 at 01:05

3 Answers3

9

@Sj comment solved it for me, copying it into an answer in case someone will miss it:

Did u tried storing the peripheral object in an ivar and then connect to it? ie self.discoveredPeripheral = peripheral; [self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];

amit
  • 1,671
  • 23
  • 44
3

I am using TemperatureSensor iOS sample app which didDiscoverPeripheral did not called. By reference the flow of Heart Rate Monitor Mac sample app. Please make sure the run sequence of code is:

1. centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
2. - (void) centralManagerDidUpdateState:(CBCentralManager *)central
3. [centralManager scanForPeripheralsWithServices:... 

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:... will be called afterwards.

oOEric
  • 959
  • 1
  • 9
  • 21
0

Call the connect method only once and wait for the connection delegate response. The above code which you posted will call the connect method more often (when ever it discover the peripheral)

if(!self.detectedBLE){ [self.centralManager stopScan]; self.detectedBLE = peripheral; [self.centralManager connectPeripheral:self.detectedBLE options:nil]; }

Bala Prabu
  • 11
  • 1