DidDiscoverPeripheral delegate method is not called

拟墨画扇 提交于 2019-12-14 01:41:52

问题


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...

来源:https://stackoverflow.com/questions/17337604/diddiscoverperipheral-delegate-method-is-not-called

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