Joshua Noble
Joshua Noble

Reputation: 894

CoreBluetooth scanForPeripheralsWithServices no longer discovers devices

After updating to OSX 12 and revisiting an old project, I find that my CBCentralManager interfacing code no longer works:


// this does get called
- (void) controlSetup
{
    NSLog(@"BLE::controlSetup");
    self.CM = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (int) findBLEPeripherals:(int) timeout
{
    if (self.CM.state != CBManagerStatePoweredOn)
    {
        return -1;
    }
    
    [NSTimer scheduledTimerWithTimeInterval:(float)timeout target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO];

    //[self.CM scanForPeripheralsWithServices:nil options:nil]; // doesn't work
    // also doesn't work
    NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
    [self.CM scanForPeripheralsWithServices:nil options:scanOptions];
    return 0; // Started scanning OK
}

// rest of CBCentralManagerDelegate methods omitted for brevity


// is no longer triggered:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"BLE::didDiscoverPeripheral didDiscoverPeripheral");
}

The CBCentralManager property is set up as strong and nonatomic, so I don't think it's getting collected anywhere:

@property (strong, nonatomic) CBCentralManager *CM;

This project is usually a plugin for Unity and until OSX12.0 was working as expected when run in the editor and in applications built in Unity. Does anyone have thoughts on why this might have suddenly stopped working or what I might try?

Upvotes: 1

Views: 329

Answers (1)

Rob Napier
Rob Napier

Reputation: 299275

This may be a bug fixed in 12.3.

However, if possible, you should be scanning for your specific list of services rather than nil. Scanning for nil is generally only appropriate for general-purpose BLE scanners. I expect CoreBluetoth on macOS to continue to move towards the iOS approach, where scanning for nil service is very restricted, so if you don't need it, now would be a good time to remove it.

Upvotes: 1

Related Questions