【问题标题】:iOS CoreBluetooth passively check if Bluetooth is enabled without prompting user to turn Bluetooth oniOS CoreBluetooth 被动检查蓝牙是否启用而不提示用户打开蓝牙
【发布时间】:2013-05-01 01:00:24
【问题描述】:

场景如下。我有一个已经部署的应用程序正在被人们使用。我正在考虑实施一些实验性的低功耗蓝牙功能,但在此之前,我想对已经开启蓝牙/完全开启蓝牙的用户数量进行“野外”调查时间。因此,我只想在后台检查这一点,然后将已开启蓝牙的用户数量的统计信息发送回服务器,而实际上并未使用蓝牙通信。

我已经在我的应用程序中成功地实例化了一个CBCentralManager,并且可以在实例化之后立即检索蓝牙状态,并在通过centralManagerDidUpdateState: 更新蓝牙状态时检索蓝牙状态。所以这一切都很好。我遇到的问题是,如果状态为CBCentralManagerStatePoweredOff,则设备会发出警报,提示用户“打开蓝牙以允许 [应用程序名称] 连接到附件”。这是我试图避免的提示。在这个阶段,我只想调查打开蓝牙的用户数量——我不想使用蓝牙连接。

那么,如果您不打算使用蓝牙连接,有没有办法在不提示用户的情况下检查蓝牙状态?

【问题讨论】:

    标签: ios core-bluetooth bluetooth-lowenergy


    【解决方案1】:

    iOS 7 在CBCentralManager 的创建中有一个新的options 参数来关闭它。

    我喜欢早期 iOS 的解决方案。

    【讨论】:

    • 不错的发现。你指的是CBCentralManagerOptionShowPowerAlertKey吗?如果您决定扩展您的答案以指定将其传递给哪个调用/链接到更多信息,我会将此标记为已接受的答案。否则,当我有机会再次更详细地查看它时,我会对此答案进行编辑。
    • 是的,使用 CBCentralManagerOptionShowPowerAlertKey,试试 myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]}];
    【解决方案2】:

    初始化CBCentralManager时可以使用以下内容。

    NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
    self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    

    我可能没有正确解释,如果有任何疑问,请直接进入评论。

    【讨论】:

    • @Maximilian 你确定吗?它适用于我的 iOS 8.4.1 iPad Mini 2。
    • @po5i var options = [CBCentralManagerOptionShowPowerAlertKey: false] self.manager = CBCentralManager(delegate: self, queue: nil, options: options)
    • 在 iOS13 中这会提示用户访问蓝牙的权限。
    猜你喜欢
    • 2018-09-23
    • 2013-03-26
    • 2020-09-05
    • 2012-07-17
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多