您的位置:首页 > 移动开发 > IOS开发

ios判断蓝牙是否开启

2015-03-24 21:49 302 查看
在做ibeacon 模块时,首先要判断蓝牙是否开着。那么问题来了,怎么样用代码判断蓝牙开着呢?

查了API,发现CBCentralManager可以实现。

首先要完成协议 CBCentralManagerDelegate

@property CBCentralManager *centralManager;

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
//第一次打开或者每次蓝牙状态改变都会调用这个函数
if(central.state==CBCentralManagerStatePoweredOn)
{
NSLog(@"蓝牙设备开着");
_canShake=YES;
}
else
{
NSLog(@"蓝牙设备关着");

UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@"亲,请打开蓝牙哦" message:@"打开蓝牙摇一摇,优惠就会出现哦~" delegate:self cancelButtonTitle:@"好的。" otherButtonTitles: nil];
[alterView show];
_canShake=NO;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  蓝牙 ios 代码