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)
使用此代码警告不再出现,我希望有所帮助!
在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!