检测iPad上的蓝牙是否已启用iOS 7

1 bluetooth objective-c ipad ios ios7

我只是试图检测,当我的应用程序在iPad上启动时,是否启用了设备上的蓝牙.

具体来说,我想在我的iPad上启动应用程序,让应用程序检查后台设备上是否启用了蓝牙,如果是,应用程序什么都不做,但如果蓝牙被禁用,它会触发一个警告提示用户打开蓝牙.我已经做过研究调查,但未能找到一个清晰简洁的答案.任何帮助将非常感激.

小智 5

如果您在应用程序中实例化CBCentralManager,ios将自动提示用户从"设置"页面启用蓝牙.

在viewDidLoad或一些顶级函数中添加以下内容:

_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
Run Code Online (Sandbox Code Playgroud)

您可以覆盖'centralManagerDidUpdateState'来捕获回调:

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBCentralManagerStatePoweredOn) {
        //Do what you intend to do
    } else if(central.state == CBCentralManagerStatePoweredOff) {
        //Bluetooth is disabled. ios pops-up an alert automatically
    }
}
Run Code Online (Sandbox Code Playgroud)

  • #import <CoreBluetooth/CoreBluetooth.h> (2认同)