此代码允许确定当前的蓝牙状态:
CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil];
switch ([testBluetooth state]) {....}
但是,当[[CBCentralManager alloc] init...]发生时,如果蓝牙关闭,系统会向用户弹出警报。
有没有办法在不打扰我的用户的情况下检查蓝牙状态?
我从一位苹果开发人员那里得到以下回复:在 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];
使用此代码,警告不再出现,希望对您有所帮助!
在 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!
我已使用以下代码禁用iOS 8 及更高版本的警报
self.bluetoothManager = [[CBCentralManager alloc]
initWithDelegate:self
queue:dispatch_get_main_queue()
options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];
[self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
通过结合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];
}
我只在 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
当您的应用程序在支持蓝牙 LE 且蓝牙被禁用的 iOS 设备上运行时,目前无法禁用此警报。提供一种禁用警报的方法将是一个增强请求。因此,Apple 对这项增强功能提出的要求越多越好。