CBCentralManager scanForPeripheralsWithServices:nil没有返回任何结果

Cyp*_*ian 1 bluetooth objective-c ios bluetooth-lowenergy

我正在尝试显示所有可用的BLE信标.我有一些Estimote和Kontakt.io信标,由于某种原因,下面的BLE扫描代码找不到任何一个.

我经历了与BLE发现有关的所有可能的SO问题,并且代码与其他地方完全一样.

应用源代码在这里

- (void)viewDidLoad {
    [super viewDidLoad];

     self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}


/*
 Request CBCentralManager to scan for all available services
 */
- (void) startScan
{
    NSLog(@"Start scanning");

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

    [self.manager scanForPeripheralsWithServices:nil options:options];
}
Run Code Online (Sandbox Code Playgroud)

永远不会调用此委托方法

/*
 Invoked when the central discovers bt peripheral while scanning.
 */
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)aPeripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{

    NSLog(@"THIS NEVER GETS CALLED");

}
Run Code Online (Sandbox Code Playgroud)

Pau*_*w11 7

iBeacon不作为外围设备访问 - 它们是信标.它们通过Core Location框架处理,而不是Core Bluetooth框架.

可能存在可由Core Bluetooth检测到的信标通告的供应商特定服务.

您的代码不会等到CBCentralManager处于开机状态.我做了以下更改,它适用于iOS 7.1和iOS8 -

- (void)viewDidLoad {
    [super viewDidLoad];

     self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}

- (void) startScan
{
    NSLog(@"Start scanning");

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

    [self.manager scanForPeripheralsWithServices:nil options:options];

}


- (BOOL) isLECapableHardware
{
    NSString * state = nil;

    switch ([self.manager state])
    {
        case CBCentralManagerStateUnsupported:
            state = @"The platform/hardware doesn't support Bluetooth Low Energy.";
            break;
        case CBCentralManagerStateUnauthorized:
            state = @"The app is not authorized to use Bluetooth Low Energy.";
            break;
        case CBCentralManagerStatePoweredOff:
            state = @"Bluetooth is currently powered off.";
            break;
        case CBCentralManagerStatePoweredOn:
            [self startScan];
            return TRUE;
        case CBCentralManagerStateUnknown:
        default:
            return FALSE;

    }

    NSLog(@"Central manager state: %@", state);

    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setMessage:state];
    [alert addButtonWithTitle:@"OK"];
    [alert show];

    return FALSE;
}
Run Code Online (Sandbox Code Playgroud)