我只是想检测一下,当我的应用程序在iPad上启动时,设备上的蓝牙是否已启用。
具体来说,我想在我的iPad上启动这个应用程序,让应用程序检查是否在后台的设备上启用蓝牙,如果是的话,应用程序什么也不做,但是如果蓝牙被禁用,它会触发一个提示用户打开蓝牙的警报。我对此进行了调查研究,但未能找到一个清晰而简洁的答案来回答我的问题。任何帮助都将不胜感激。
发布于 2014-07-23 19:00:23
如果您在应用程序中实例化CBCentralManager,ios将自动提示用户从设置页面启用蓝牙。
在viewDidLoad或顶级函数中添加以下内容:
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];您可以重写“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
}
}发布于 2017-04-18 17:31:04
对于iOS 10,需要稍微更新接受的答案。
CBCentralManagerStatePoweredOn和CBCentralManagerStatePoweredOff已被废弃,应由CBManagerStatePoweredOn和CBManagerStatePoweredOff取代。
更新代码:
- (void)centralManagerDidUpdateState:(CBCentralManager*)aManager
{
if( aManager.state == CBManagerStatePoweredOn )
{
//Do what you intend to do
}
else if( aManager.state == CBManagerStatePoweredOff )
{
//Bluetooth is disabled. ios pops-up an alert automatically
}
}https://stackoverflow.com/questions/24916182
复制相似问题