【问题标题】:CLBeaconRegion, how to turn off warning: Turn On Bluetooth to Allow * to Connect to AccessoriesCLBeaconRegion,如何关闭警告:打开蓝牙允许*连接到附件
【发布时间】:2023-03-30 21:19:01
【问题描述】:

我们有一个项目正在使用 CoreLocation 区域来监控 iBeacon 区域在应用后台的进入/退出。 CLBeaconRegion (CLRegion)、CLBeacon 等。CLLocationManager 在进入 CLBeacon (iBeacon) 区域时返回回调。它是一个轻量级的封装在下面的 bluetoothManager 周围。

// various CLLocation delegate callback examples
- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region;
- (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region;

我们遇到的问题是,当用户没有打开蓝牙时,Iphone 会定期发出系统级警告“打开蓝牙以允许“APP_NAME”连接其他配件”。这种情况经常发生,今天早上我已经得到了 4 次,因为应用程序在后台运行。 CLLocationManager 可能正在尝试监视这些 CLBeaconRegion,但蓝牙已关闭,因此无法执行此操作。

另一篇文章提到 CBCentralManager 有一个属性 CBCentralManagerOptionShowPowerAlertKey,它允许禁用此警告。

iOS CoreBluetooth passively check if Bluetooth is enabled without prompting user to turn Bluetooth on

不幸的是,我发现无法访问底层蓝牙或任何 CBCentralManager 引用来使用它。

有什么方法可以禁用此警告以进行 CLBeaconRegion 监控?

【问题讨论】:

  • 您是在实例化一个 CLLocationManager 实例还是在使用其他库?据我所知,您不应该直接使用 CoreLocation 得到这个提示
  • 我们正在使用 CLLocationManager 并实现它的委托方法
  • 我用一个使用信标监控的应用程序进行了测试,我得到一个稍微不同的对话框,通知用户如果启用蓝牙,定位精度将会提高。我只有在第一次在蓝牙关闭的情况下运行应用程序时才得到这个。您显示的对话框是我希望使用核心蓝牙而不是核心位置的对话框。你运行的是什么版本的 iOS?
  • 我刚刚尝试了应用商店中的另一个 iBeacon 应用,它显示的对话框与您看到的相同,因此可能取决于编译该应用所针对的 API 之类的。可能避免对话的唯一方法就是使用您链接到的答案中的技术分配一个 CBCentral 并检测蓝牙是打开还是关闭。如果它关闭,请不要初始化信标区域监控。
  • 仅供参考,我正在运行最新的 iOS,大约 8.1。我制定了一个逻辑上与您关于 CBCentral 和检测蓝牙的上一条评论类似的解决方案。

标签: ios core-bluetooth clbeacon clbeaconregion


【解决方案1】:

我制定了一个使用 CoreBluetooth 和 CBCentralManager 来检测、停止和启动蓝牙使用的解决方案。以下是大部分代码,以及在开始之前进行的初始检查以查看它是否已打开。它通过保证蓝牙关闭时不使用信标来抑制错误提示。如果它被禁用,则停止信标。警告就这样消失了。不幸的是,我们无法以编程方式实际启用/禁用蓝牙。

// initialize in viewdidLoad, etc
_bluetoothManager =  [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:@NO}];


// bluetooth manager state change
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(central.state)
    {
        case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
        case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
        case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
        case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
        case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
        default: stateString = @"State unknown, update imminent."; break;
    }

    if(_bluetoothState != CBCentralManagerStateUnknown && _bluetoothState != CBCentralManagerStatePoweredOn && central.state == CBCentralManagerStatePoweredOn){
         NSLog(@"BEACON_MANAGER: Bluetooth just enabled. Attempting to start beacon monitoring.");
        _forceRestartLM = YES; // make sure we force restart LMs on next update, since they're stopped currently and regions may not be updated to trigger it
        [self startBeaconMonitoring];
    }

    if(_bluetoothState != CBCentralManagerStateUnknown && _bluetoothState == CBCentralManagerStatePoweredOn && central.state != CBCentralManagerStatePoweredOn)    {
        NSLog(@"BEACON_MANAGER: Bluetooth just disabled. Attempting to stop beacon monitoring.");
        [self stopBeaconMonitoring];
    }
}

【讨论】:

  • 您的[self startBeaconMonitoring][self stopBeaconMonitoring] 方法是什么?我很想在我的应用中使用它们!
  • // 完全初始化locationManager,并启动。然后这样做: NSUUID uuid = [[NSUUID alloc] initWithUUIDString:@"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX"]; CLBeaconRegion region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"identifier"]; region.notifyEntryStateOnDisplay = YES; [locationManager startMonitoringForRegion:region];
  • 这就是基础,然后你有标准的 locationManager 委托方法来响应信标事件。停止只是停止 locationManager 和/或删除信标监视器项目。这就是我可以放入 cmets 的所有代码
  • 哦,所以您实际上并没有启用或禁用蓝牙?
猜你喜欢
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 2011-08-09
  • 2011-01-26
  • 2019-07-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多