3

I am working with iphone5,and scanning for the device with the UUID.and i want to get list of available bluetooth device in my apps,but didDiscoverPeriphral delegate method is not call. here i give my code.

- (id)init
{
    NSLog(@"hello init");
    if ((self = [super init]))
    {
        CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    }
    return self;
}

- (void)viewDidLoad
{   CM = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
    isOn=NO;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (int) scanForPeripherals
{

    if (self->CM.state != CBCentralManagerStatePoweredOn)
    {
        NSLog(@"CoreBluetooth is %s",[self centralManagerStateToString:self->CM.state]);
        return -1;
    }

    [self->CM scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];
    return 0;
}

- (const char *) centralManagerStateToString: (int)state
{
    switch(state)
    {
        case CBCentralManagerStateUnknown:
            return "State unknown (CBCentralManagerStateUnknown)";
        case CBCentralManagerStateResetting:
            return "State resetting (CBCentralManagerStateUnknown)";
        case CBCentralManagerStateUnsupported:
            return "State BLE unsupported (CBCentralManagerStateResetting)";
        case CBCentralManagerStateUnauthorized:
            return "State unauthorized (CBCentralManagerStateUnauthorized)";
        case CBCentralManagerStatePoweredOff:
            return "State BLE powered off (CBCentralManagerStatePoweredOff)";
        case CBCentralManagerStatePoweredOn:
            return "State powered up and ready (CBCentralManagerStatePoweredOn)";
        default:
            return "State unknown";
    }

    return "Unknown state";
}


- (void) connectPeripheral:(CBPeripheral *)peripheral {
    printf("Connecting to peripheral with UUID : %s\r\n",[self UUIDToString:peripheral.UUID]);

    self->activePeripheral = peripheral;
    self->activePeripheral.delegate = self;
    [self->CM connectPeripheral:self->activePeripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
    NSLog(@"peripheral :%@",peripheral);
}


-(const char *) UUIDToString:(CFUUIDRef)UUID
{
    if (!UUID)
        return "NULL";

    CFStringRef s = CFUUIDCreateString(NULL, UUID);
    return CFStringGetCStringPtr(s, 0);
}


#pragma mark -Central manager delegate method

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{

    NSLog(@"hits it");
    if (central.state != CBCentralManagerStatePoweredOn) {
        return;
    }

    isOn=YES;
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Received periferal :%@",peripheral);
    NSLog(@"Ad data :%@",advertisementData);


}


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    NSLog(@"Connected peripheral %@",peripheral);
}


- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    NSLog(@"Error occured :%@",[error localizedDescription]);
}



- (IBAction)scanDevices:(id)sender {
    //isOn=YES;
    if (isOn) {

        [self scanForPeripherals];
    }
}

thanks...

  • Did you tried first with `[self->CM scanForPeripheralsWithServices:nil options:yourOptions]`? Does `scanForPeripherals` returns 0? – Larme Jun 27 '13 at 08:25
  • Seems like a silly question, but do you have a device running as a peripheral that should be showing up? – mwright Oct 09 '13 at 20:40
  • 2
    Did you resolve this? I'm running into the same issue. didDiscoverPeripheral is never called, even with scanForPeripheralsWithServices:nil – Cris Feb 21 '14 at 07:36
  • 2
    Had this been resolved? I have the same issue. My device (polar H6) is visible within the app LightBlue (a test app available in the App Store) but can not see it in my own app. – Brabbeldas Oct 19 '14 at 12:49
  • Please check this solution: http://stackoverflow.com/questions/28167804/why-use-corebluetooth-connectperipheral-did-not-call-delegate-methods-in-ios8/31961410#31961410 – oOEric Aug 12 '15 at 09:38

0 Answers0