首先在build phase中添加CoreBluetooth.framework
然后在你的.m文件中#import <CoreBluetooth/CoreBluetooth.h>
初始化
- (CBCentralManager *)bluetoothManager {
if (_bluetoothManager == nil) {
_bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
return _bluetoothManager;
}
实现代理方法
-
(void)centralManagerDidUpdateState:(CBCentralManager *)central {
UIView *mainView = [[UIApplication sharedApplication].windows firstObject];switch (central.state) {
case CBCentralManagerStatePoweredOff:{
[mainView makeToast:@"蓝牙没有开启,在设置中打开蓝牙"];
}
break;
case CBCentralManagerStatePoweredOn:
break;
case CBCentralManagerStateResetting:
break;
case CBCentralManagerStateUnauthorized:
break;
case CBCentralManagerStateUnknown:
break;
case CBCentralManagerStateUnsupported:
[mainView makeToast:@"当前设备不支持蓝牙"];
break;
default:
break;
}
}
通过这个代理方法就可以得到当前设备的蓝牙状态了