检查蓝牙是否启用?

我只想做一个简单的检查是否在设备上启用蓝牙。

我不想改变应用程序内部的状态(或根本不改变),使用私有API,越狱设备,或者做任何会导致苹果拒绝应用程序的事情。

我只想知道蓝牙是否打开。

任何人都可以解释这一点吗? 有没有苹果允许的方式来做到这一点?

在阅读了无数的文章和文档之后,我十分清楚苹果在蓝牙(除此之外)方面的限制性很强。

如果您只能通过链接到文档和/或关于学习Objective-C,阅读文档等方面的讽刺评论来解决这个问题,那么请不要回应。

我唯一能find的方式就是使用私有框架(比如蓝牙pipe理器),这只适用于越狱应用程序…而苹果将拒绝使用私有框架的应用程序。 我相信,即使对付他们的ToS,也无法用蓝牙来做任何事情,所以你不在那里。

这里似乎有一个答案 – 使用核心蓝牙框架

但是,这个答案只适用于iOS 5.0及更高版本。 我自己没有testing过,但是如果我发现它可以工作的话,会返回并添加反馈。

不幸的是,SDK不公开蓝牙方法。

可能有办法通过使用无证方法来做到这一点,但我们都知道那里的问题。

现在可以使用iOS 7中的CBCentralManager对其进行检查,并使用设置的CBCentralManagerOptionShowPowerAlertKey选项对其进行初始化。

CBCentralManagerOptionShowPowerAlertKey键,可以传递给CBCentralManager上的initWithDelegate:queue:options:方法,这将导致iOS启动中央pipe理器并且不会提示用户启用蓝牙。

张贴在这里: http : //chrismaddern.com/determine-whether-bluetooth-is-enabled-on-ios-passively/

对于iOS9 +,您可以在这里查看我的答案。

 #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]; }