iOS - 检查蓝牙是否打开而没有向用户弹出系统警报弹出窗口

Ole*_*yuk 12 alert bluetooth status ios

此代码允许确定当前的蓝牙状态:

CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil];


switch ([testBluetooth state]) {....}
Run Code Online (Sandbox Code Playgroud)

但是,当[[CBCentralManager alloc] init ...]发生时,如果蓝牙关闭,系统会向用户弹出警报.

有没有办法检查蓝牙状态而不打扰我的用户?

Ali*_*bas 20

我从苹果开发者处获得以下响应:在iOS7中,该CBCentralManagerOptionShowPowerAlertKey选项允许您禁用此警报.

如果在初始化时有CBCentralManager,则可以使用该方法 initWithDelegate:queue:options

例:

在我的.h文件中,我有一个 CBCentralManager * manager

在.m文件中:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];

_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

[_manager scanForPeripheralsWithServices:nil options:nil];
Run Code Online (Sandbox Code Playgroud)

使用此代码警告不再出现,我希望有所帮助!


Ale*_*fir 8

在swift中,您可以在func中的app委托中写下这两行:didFinishLaunchingWithOptions launchOptions

    self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false])
    self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil)
Run Code Online (Sandbox Code Playgroud)

您的bCentralManger应声明为:

private var bCentralManger:CBCentralManager!


Gat*_*ato 1

当您的应用程序在支持蓝牙 LE 且禁用蓝牙的 iOS 设备上运行时,当前无法禁用此警报。提供禁用警报的方法将是一个增强请求。因此,苹果收到的有关此增强功能的请求越多越好。