1

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?

Joshua Noble
  • 830
  • 2
  • 12
  • 26

1 Answers1

1

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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Interesting. I had this originally: ``` NSArray *services = @[[CBUUID UUIDWithString:BLEUart_SERVICE]]; [self.CM scanForPeripheralsWithServices:services options:scanOptions]; ``` but it also wasn't working. I wonder if this is an issue with running the executable as a plugin via Unity? – Joshua Noble Feb 01 '22 at 00:16
  • The bug linked I believe is related to background scanning at all, and running under Unity would almost certainly be background scanning. Do check your logs and make sure you don't need to set the `Privacy - Bluetooth Always Usage Description` (NSBluetoothAlwaysUsageDescription) field on macOS. (I don't set up new macOS BLE projects very often, and I don't remember if it's currently required.) – Rob Napier Feb 01 '22 at 13:52
  • 1
    Adding NSBluetoothAlwaysUsageDescription Some text NSBluetoothPeripheralUsageDescription Some text to the Info.plist for a generated XCode project works. There isn't a way to generate an app right from Unity that does this, so that's a pain but at least there is a path. – Joshua Noble Feb 01 '22 at 15:41