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
    }
}

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容