最近接到新的硬件項目倦青,需要用戶手動去開啟藍牙設(shè)置。而做項目時盹舞,習(xí)慣性是調(diào)用系統(tǒng)的提示框产镐。
對于我這種不太喜歡看蘋果文檔的人來說,有時候有點難找到相關(guān)的資料踢步。進入正題吧癣亚。上圖,上代碼获印!
@interface HNGDevice()
@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) UIAlertController *alertC;
@end
@implementation HNGDevice
+ (instancetype)shareInstance;// 設(shè)置為單例
{
? ? staticHNGDevice*device =nil;
? ? staticdispatch_once_tonceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? device = [[HNGDevicealloc]init];
? ? });
? ? returndevice;
}
- (instancetype)init
{
? ? self= [superinit];
? ? if(self) {
? ? ? ? [self subPropertiesInit]; // 初始化屬性
? ? }
? ? return self;
}
- (void)subPropertiesInit {? // 初始化屬性
? ? // 初始化管理者
? ? // CBCentralManagerOptionShowPowerAlertKey
? ? //_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
? ? _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]}];
}
#pragma mark -- CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager*)central
{
? ? switch(central.state) {
? ? ? ? case CBManagerStateUnknown:
? ? ? ? ? ? NSLog(@"central.state = CBManagerStateUnknown");
? ? ? ? ? ? break;
? ? ? ? case CBManagerStateResetting:
? ? ? ? {
? ? ? ? ? ? [self showAlertViewTitle:@"Note!" message:@"Your apple device has bluetooth reset, please close the APP and reopen." rescan:NO];
? ? ? ? ? ? NSLog(@"central.state = CBManagerStateResetting .........iPhone正在重置藍牙");
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case CBManagerStateUnsupported:
? ? ? ? ? ? NSLog(@"central.state = CBManagerStateUnsupported");
? ? ? ? ? ? break;
? ? ? ? case CBManagerStateUnauthorized:
? ? ? ? ? ? NSLog(@"central.state = CBManagerStateUnauthorized");
? ? ? ? ? ? break;
? ? ? ? case CBManagerStatePoweredOff: {
? ? ? ? ? ? NSLog(@"centralManager 未開啟藍牙..");
? ? ? ? ? ? // 代理監(jiān)控藍牙未開啟,狀態(tài)改為正在掃描中
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case CBManagerStatePoweredOn:
? ? ? ? {
? ? ? ? ? ? NSLog(@"centralManager 已開啟藍牙..");
?? ? ? ?}
? ? ? ? ? ? break;
? ? }
}
如上述雾,習(xí)慣性用第一個實例方法,這個是默認(rèn)系統(tǒng)彈出提示框的:
當(dāng)你不需要這自動提示框時,就寫第二個實例方法:
然后這樣就ok,下面繼續(xù)做別的處理匣掸。