iOS – 检查蓝牙是否打开,系统警告popup给用户
此代码可以确定当前的蓝牙状态:
CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil]; switch ([testBluetooth state]) {....}
但是,当[[CBCentralManager alloc] init …]发生时,如果蓝牙closures,系统会popup一个警告给用户。
有没有办法检查蓝牙状态,而不会打扰我的用户?
我从苹果开发者处得到了以下回应:在iOS7中, CBCentralManagerOptionShowPowerAlertKey
选项可以让你禁用这个警报。
如果您在初始化CBCentralManager时有一个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];
有了这个代码警告不再出现,我希望有帮助!
在swift中,你可以在你的应用程序委托里面写func:didFinishLaunchingWithOptions launchOptions
self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false]) self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil)
你的bCentralManger应该被声明为:
私人变种bCentralManger:CBCentralManager!
当您的应用程序在支持蓝牙LE的iOS设备上运行并且禁用了蓝牙时,目前无法禁用此警报。 这将是一个增强请求,提供一种手段来禁用警报。 所以苹果获得这个提升的要求越多越好。
我已经使用下面的代码来禁用iOS 8及以上版本的警报
self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue() options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}]; [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
我只在iOS 9上testing过,所以也许有人可以testing一个较老的操作系统设备。
除了一件事之外,我们通常都会做一切事情,而不是在viewDidLoad
中设置CBCentralManager
委托,直到我们需要它的时候,在下面的示例中,我的WKWebView
加载完成后调用此方法,因为我的Web视图的每个页面都有可能需要使用蓝牙我把这个在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
通过结合BadPirate和Anas的答案,您可以在不显示系统警报的情况下获得蓝牙状态。
#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]; }