CoreBluetooth 蓝牙切换状态实时检测

部分业务场景会需要检测获取蓝牙的状态后进行一些处理,简单的实现了一下实时检测当前蓝牙启闭状态的方法,具体如下:

首先,引入 SDK 配置相关 api 方法

// 蓝牙 配置代理 CBCentralManagerDelegate
#import <CoreBluetooth/CoreBluetooth.h> 

其次,配置 plist 文件

Privacy - Bluetooth Always Usage Description
Privacy - Bluetooth Peripheral Usage Description

再其次,初始化

#pragma mark - 检测蓝牙
/// 检测蓝牙
- (void)bluetoothDetection {
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
}

最后,实现代理方法,进行实时检测并处理相关实际业务

#pragma mark - 蓝牙代理 CLLocationManagerDelegate
/// 实时监听蓝牙状态变更回调
/// - Parameter central: 控制中心
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBManagerStatePoweredOn) {
        NSLog(@"[蓝牙检测] - 状态 - 开启");
        // do something
    } else if (central.state == CBManagerStatePoweredOff) {
        NSLog(@"[蓝牙检测] - 状态 - 关闭");
        // do something
    }
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

猜你喜欢

转载自blog.csdn.net/survivorsfyh/article/details/128969810