最近項目中需要監(jiān)測藍(lán)牙狀態(tài),所以本文只是簡單的封裝了藍(lán)牙的開啟關(guān)閉等.
封裝類.h
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
@class LKBlueToothCentralManager;
@protocol LKBlueToothCentralManagerDelegate <NSObject>
@optional
/** 藍(lán)牙初始化中 **/
- (void)blueToothStateChangeToInitializationWithManager:(LKBlueToothCentralManager *)manager;
/** 藍(lán)牙重置 **/
- (void)blueToothStateChangeToResettingWithManager:(LKBlueToothCentralManager *)manager;
/** 設(shè)備不支持藍(lán)牙 **/
- (void)blueToothStateChangeToNotSupportedWithManager:(LKBlueToothCentralManager *)manager;
/** 設(shè)置未授權(quán)藍(lán)牙 **/
- (void)blueToothStateChangeToWantAuthorityWithManager:(LKBlueToothCentralManager *)manager;
/** 未打開藍(lán)牙狀態(tài) **/
- (void)blueToothStateChangeToUnopenedWithManager:(LKBlueToothCentralManager *)manager;
/** 藍(lán)牙開啟成功 **/
- (void)blueToothStateChangeToDidOpenWithManager:(LKBlueToothCentralManager *)manager;
@end
typedef NS_ENUM(NSInteger)
{
LKBlueToothManagerStateInitialization = 0, // *狀態(tài)未知
LKBlueToothManagerStateResetting = 1, // *狀態(tài)重置
LKBlueToothManagerStateNotSupported = 2, // *設(shè)備不支持
LKBlueToothManagerStateWantAuthority = 3, // *設(shè)為未授權(quán)
LKBlueToothManagerStateUnopened = 4, // *設(shè)備未打開
LKBlueToothManagerStateIsOpen = 5, // *設(shè)備已打開
}LKBlueToothManagerState;
@interface LKBlueToothCentralManager : CBCentralManager <CBCentralManagerDelegate>
/** 添加代理 **/
+ (void)addDelegate:(id<LKBlueToothCentralManagerDelegate>)delegate;
/** 獲取當(dāng)前用戶藍(lán)牙狀態(tài) **/
+ (LKBlueToothManagerState)getCurrentBlueToothState;
/** 獲取當(dāng)前用戶藍(lán)牙狀態(tài)字符串 **/
+ (NSString *)getCurrentBlueToothStateStr;
@end
封裝類.m
#import "LKBlueToothCentralManager.h"http://這里是類名,改成自己的
@interface LKBlueToothCentralManager ()
{
id<LKBlueToothCentralManagerDelegate> _myDelegate;
}
@end
@implementation LKBlueToothCentralManager
static LKBlueToothCentralManager *_manager;
+(void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_manager = [[LKBlueToothCentralManager alloc] init];
_manager.delegate = _manager.self;
});
}
+ (void)addDelegate:(id<LKBlueToothCentralManagerDelegate>)delegate
{
_manager -> _myDelegate = delegate;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state) {
case 0:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToInitializationWithManager:)])
{
[_myDelegate blueToothStateChangeToInitializationWithManager:_manager];
}
break;
case 1:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToResettingWithManager:)])
{
[_myDelegate blueToothStateChangeToResettingWithManager:_manager];
}
break;
case 2:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToNotSupportedWithManager:)])
{
[_myDelegate blueToothStateChangeToNotSupportedWithManager:_manager];
}
break;
case 3:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToWantAuthorityWithManager:)])
{
[_myDelegate blueToothStateChangeToWantAuthorityWithManager:_manager];
}
break;
case 4:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToUnopenedWithManager:)])
{
[_myDelegate blueToothStateChangeToUnopenedWithManager:_manager];
}
break;
case 5:
if ([_myDelegate respondsToSelector:@selector(blueToothStateChangeToDidOpenWithManager:)])
{
[_myDelegate blueToothStateChangeToDidOpenWithManager:_manager];
}
break;
default:
break;
}
}
+ (LKBlueToothManagerState)getCurrentBlueToothState
{
switch (_manager.state) {
case 0:
return LKBlueToothManagerStateInitialization;
case 1:
return LKBlueToothManagerStateResetting;
case 2:
return LKBlueToothManagerStateNotSupported;
case 3:
return LKBlueToothManagerStateWantAuthority;
case 4:
return LKBlueToothManagerStateUnopened;
case 5:
return LKBlueToothManagerStateIsOpen;
default:
return -1;
}
}
+ (NSString *)getCurrentBlueToothStateStr
{
switch (_manager.state) {
case 0:
return @"初始化中";
case 1:
return @"設(shè)備重置";
case 2:
return @"設(shè)備不支持";
case 3:
return @"設(shè)備未授權(quán)";
case 4:
return @"未打開";
case 5:
return @"已打開";
default:
return @"";
}
}
@end
食用方法 (′?ω?`)
#import <CoreBluetooth/CoreBluetooth.h>//系統(tǒng)的頭文件
#import "LKBlueToothCentralManager.h"http://自己封裝的類的頭文件
//點擊屏幕獲得藍(lán)牙狀態(tài)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"藍(lán)牙狀態(tài)字符串%@",[LKBlueToothCentralManager getCurrentBlueToothStateStr]);
NSLog(@"藍(lán)牙狀態(tài)%zd--主要用來對比自定的枚舉",[LKBlueToothCentralManager getCurrentBlueToothState]);
}
//如果要使用代理來監(jiān)聽藍(lán)牙狀態(tài)變化
//先遵守自定的協(xié)議<LKBlueToothCentralManagerDelegate>
//合適的地方設(shè)置代理[LKBlueToothCentralManager addDelegate:self];//如viewDidLoad方法
//下面就是監(jiān)聽方法了
/** 藍(lán)牙初始化中 **/
- (void)blueToothStateChangeToInitializationWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"藍(lán)牙初始化中");
}
/** 藍(lán)牙重置 **/
- (void)blueToothStateChangeToResettingWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"藍(lán)牙重置");
}
/** 設(shè)備不支持藍(lán)牙 **/
- (void)blueToothStateChangeToNotSupportedWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"設(shè)備不支持藍(lán)牙");
}
/** 設(shè)置未授權(quán)藍(lán)牙 **/
- (void)blueToothStateChangeToWantAuthorityWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"設(shè)置未授權(quán)藍(lán)牙");
}
/** 未打開藍(lán)牙狀態(tài) **/
- (void)blueToothStateChangeToUnopenedWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"未打開藍(lán)牙狀態(tài)");
}
/** 藍(lán)牙開啟成功 **/
- (void)blueToothStateChangeToDidOpenWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"藍(lán)牙開啟成功");
}