iOS - 检查蓝牙是否打开而没有向用户弹出系统警报 [英] iOS - check if bluetooth is on without system alert popup to user

查看:65
本文介绍了iOS - 检查蓝牙是否打开而没有向用户弹出系统警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This code allows to determine current bluetooth status:

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


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

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

But, when [[CBCentralManager alloc] init...] happens, system popups an alert to user, if bluetooth is off.

有没有什么方法可以在不打扰我的用户的情况下检查蓝牙状态?

推荐答案

我从一个苹果开发者那里得到了以下回复:在 iOS7 中,CBCentralManagerOptionShowPowerAlertKey 选项可以让你禁用这个警报.

I got the following response from an apple developer : In iOS7, the CBCentralManagerOptionShowPowerAlertKey option lets you disable this alert.

如果你在初始化时有一个 CBCentralManager,你可以使用方法 initWithDelegate:queue:options

If you havea a CBCentralManager when you initialise it, you can use the method initWithDelegate:queue:options

示例:

在我的 .h 文件中,我有一个 CBCentralManager * manager

In my .h file i have a 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];

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

With this code the warning no longer appears, I hope that helps !

这篇关于iOS - 检查蓝牙是否打开而没有向用户弹出系统警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆