CoreBluetooth scanForPeripheralsWithServices 不再发现设备

CoreBluetooth scanForPeripheralsWithServices no longer discovers devices

更新到 OSX 12 并重新访问一个旧项目后,我发现我的 CBCentralManager 接口代码不再有效:


// 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");
}

CBCentralManager 属性 设置为强非原子,所以我认为它不会被收集到任何地方:

@property (strong, nonatomic) CBCentralManager *CM;

这个项目通常是 Unity 的插件,直到 OSX12.0 在 运行 在编辑器和 Unity 内置的应用程序中按预期工作。有没有人想过为什么这可能突然停止工作或者我可能会尝试什么?

这可能是 bug fixed in 12.3

但是,如果可能,您应该扫描特定的服务列表而不是 nil。扫描 nil 通常只适用于 general-purpose BLE 扫描仪。我希望 macOS 上的 CoreBluetoth 继续朝着 iOS 方法发展,其中扫描 nil 服务非常受限,因此如果您不需要它,现在是删除它的好时机。