14

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

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


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

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

有没有办法在不打扰我的用户的情况下检查蓝牙状态?

4

6 回答 6

22

我从一位苹果开发人员那里得到以下回复:在 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];

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

于 2013-10-21T20:45:13.217 回答
9

在 swift 中,您可以在 func 内的应用程序委托中编写这两行:didFinishLaunchingWithOptions launchOptions

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

您的 bCentralManger 应声明为:

私人 var bCentralManger:CBCentralManager!

于 2016-02-29T12:22:21.890 回答
3

我已使用以下代码禁用iOS 8 及更高版本的警报

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

[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
于 2015-11-26T06:45:55.627 回答
3

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

#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];
}
于 2017-01-19T02:42:06.787 回答
2

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

除了一件事之外,我们通常做所有事情,而不是设置CBCentralManager委托,viewDidLoad我们将其保留到我们需要它的那一刻,在下面的示例中,一旦我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()您将看到状态发生变化。

打开蓝牙的通知似乎只想在您的应用程序的初始加载时被调用,这样做意味着您只需从centralManagerDidUpdateState

于 2016-02-24T13:30:57.207 回答
1

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

于 2012-10-19T00:26:21.677 回答