最近项目中需要监测蓝牙状态,所以本文只是简单的封装了蓝牙的开启关闭等.
封装类.h
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
@class LKBlueToothCentralManager;
@protocol LKBlueToothCentralManagerDelegate <NSObject>
@optional
/** 蓝牙初始化中 **/
- (void)blueToothStateChangeToInitializationWithManager:(LKBlueToothCentralManager *)manager;
/** 蓝牙重置 **/
- (void)blueToothStateChangeToResettingWithManager:(LKBlueToothCentralManager *)manager;
/** 设备不支持蓝牙 **/
- (void)blueToothStateChangeToNotSupportedWithManager:(LKBlueToothCentralManager *)manager;
/** 设置未授权蓝牙 **/
- (void)blueToothStateChangeToWantAuthorityWithManager:(LKBlueToothCentralManager *)manager;
/** 未打开蓝牙状态 **/
- (void)blueToothStateChangeToUnopenedWithManager:(LKBlueToothCentralManager *)manager;
/** 蓝牙开启成功 **/
- (void)blueToothStateChangeToDidOpenWithManager:(LKBlueToothCentralManager *)manager;
@end
typedef NS_ENUM(NSInteger)
{
LKBlueToothManagerStateInitialization = 0, // *状态未知
LKBlueToothManagerStateResetting = 1, // *状态重置
LKBlueToothManagerStateNotSupported = 2, // *设备不支持
LKBlueToothManagerStateWantAuthority = 3, // *设为未授权
LKBlueToothManagerStateUnopened = 4, // *设备未打开
LKBlueToothManagerStateIsOpen = 5, // *设备已打开
}LKBlueToothManagerState;
@interface LKBlueToothCentralManager : CBCentralManager <CBCentralManagerDelegate>
/** 添加代理 **/
+ (void)addDelegate:(id<LKBlueToothCentralManagerDelegate>)delegate;
/** 获取当前用户蓝牙状态 **/
+ (LKBlueToothManagerState)getCurrentBlueToothState;
/** 获取当前用户蓝牙状态字符串 **/
+ (NSString *)getCurrentBlueToothStateStr;
@end
封装类.m
#import "LKBlueToothCentralManager.h"//这里是类名,改成自己的
@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 @"设备重置";
case 2:
return @"设备不支持";
case 3:
return @"设备未授权";
case 4:
return @"未打开";
case 5:
return @"已打开";
default:
return @"";
}
}
@end
食用方法 (´・ω・`)
#import <CoreBluetooth/CoreBluetooth.h>//系统的头文件
#import "LKBlueToothCentralManager.h"//自己封装的类的头文件
//点击屏幕获得蓝牙状态
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"蓝牙状态字符串%@",[LKBlueToothCentralManager getCurrentBlueToothStateStr]);
NSLog(@"蓝牙状态%zd--主要用来对比自定的枚举",[LKBlueToothCentralManager getCurrentBlueToothState]);
}
//如果要使用代理来监听蓝牙状态变化
//先遵守自定的协议<LKBlueToothCentralManagerDelegate>
//合适的地方设置代理[LKBlueToothCentralManager addDelegate:self];//如viewDidLoad方法
//下面就是监听方法了
/** 蓝牙初始化中 **/
- (void)blueToothStateChangeToInitializationWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"蓝牙初始化中");
}
/** 蓝牙重置 **/
- (void)blueToothStateChangeToResettingWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"蓝牙重置");
}
/** 设备不支持蓝牙 **/
- (void)blueToothStateChangeToNotSupportedWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"设备不支持蓝牙");
}
/** 设置未授权蓝牙 **/
- (void)blueToothStateChangeToWantAuthorityWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"设置未授权蓝牙");
}
/** 未打开蓝牙状态 **/
- (void)blueToothStateChangeToUnopenedWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"未打开蓝牙状态");
}
/** 蓝牙开启成功 **/
- (void)blueToothStateChangeToDidOpenWithManager:(LKBlueToothCentralManager *)manager
{
NSLog(@"蓝牙开启成功");
}