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

时间:2012-09-21 15:10:31

标签: ios bluetooth alert status

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

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


switch ([testBluetooth state]) {....}

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

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

6 个答案:

答案 0 :(得分: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];

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

答案 1 :(得分:7)

在swift中你可以在func中的app delegate中写下这两行:didFinishLaunchingWithOptions launchOptions

    self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false])
    self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil)

您的bCentralManger应声明为:

private var bCentralManger:CBCentralManager!

答案 2 :(得分:2)

我使用以下代码禁用 iOS 8及以上版本

的提醒
self.bluetoothManager = [[CBCentralManager alloc]
                                      initWithDelegate:self 
                                      queue:dispatch_get_main_queue() 
                                      options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];

[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];

答案 3 :(得分:2)

通过结合BadPirate'sAnas'答案,您可以在不显示系统警报的情况下获得蓝牙状态。

#import <CoreBluetooth/CoreBluetooth.h>

@interface ShopVC () <CBCentralManagerDelegate>

@property (nonatomic, strong) CBCentralManager *bluetoothManager;

@end

@implementation ShopVC

- (void)viewDidLoad {
    [super viewDidLoad];

    if(!self.bluetoothManager)
    {
        NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
        self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    }
}

#pragma mark - CBCentralManagerDelegate

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(self.bluetoothManager.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;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state"
                                                    message:stateString
                                                   delegate:nil
                                          cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
}

答案 4 :(得分:1)

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

答案 5 :(得分:1)

我只在iOS 9上测试了这个,所以也许有人可以测试这个旧的操作系统设备。

除了一件事之外,我们通常会做所有事情,而不是设置CBCentralManager中的viewDidLoad代表,我们将其留到我们需要它的那一刻,在下面的示例中,我称之为{{1}已完成加载,并且由于我的网页视图的每个页面都可能需要使用蓝牙,我将其放在WKWebView中。

<强>夫特

WKWebView didFinishNavigation

委托在var managerBLE: CBCentralManager? func bluetoothStatus() { managerBLE = CBCentralManager(delegate: self, queue: nil, options: nil) } func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { bluetoothStatus() } func centralManagerDidUpdateState(central: CBCentralManager) { switch managerBLE!.state { case CBCentralManagerState.PoweredOff: print("Powered Off") case CBCentralManagerState.PoweredOn: print("Powered On") case CBCentralManagerState.Unsupported: print("Unsupported") case CBCentralManagerState.Resetting: print("Resetting") fallthrough case CBCentralManagerState.Unauthorized: print("Unauthorized") case CBCentralManagerState.Unknown: print("Unknown") default: break; } } 内设置的那一刻,你会看到状态改变。

启用蓝牙的通知似乎只是想在您的应用初始加载时调用,这样做意味着您只需从bluetoothStatus()获得所需内容