美文网首页iOS
iOS检测蓝牙是否开启

iOS检测蓝牙是否开启

作者: 一线码农 | 来源:发表于2016-03-22 17:10 被阅读5017次

首先在build phase中添加CoreBluetooth.framework
然后在你的.m文件中#import <CoreBluetooth/CoreBluetooth.h>
初始化

  • (CBCentralManager *)bluetoothManager {
    if (_bluetoothManager == nil) {
    _bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    }
    return _bluetoothManager;
    }

实现代理方法

  • (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    UIView *mainView = [[UIApplication sharedApplication].windows firstObject];

    switch (central.state) {
    case CBCentralManagerStatePoweredOff:{
    [mainView makeToast:@"蓝牙没有开启,在设置中打开蓝牙"];
    }
    break;
    case CBCentralManagerStatePoweredOn:
    break;
    case CBCentralManagerStateResetting:
    break;
    case CBCentralManagerStateUnauthorized:
    break;
    case CBCentralManagerStateUnknown:
    break;
    case CBCentralManagerStateUnsupported:
    [mainView makeToast:@"当前设备不支持蓝牙"];
    break;
    default:
    break;
    }
    }
    通过这个代理方法就可以得到当前设备的蓝牙状态了

相关文章

网友评论

  • 问夕阙:如果从设置那返回到APP里去 怎么再去判断蓝牙是否打开呢?
  • 等这姑娘老在我心里:我想问一下还有别的方法检测蓝牙是否开启呢,因为这样子检测 会弹出一个框让你到设置里面去开启蓝牙
    南方小金豆:你找到其他的检测方法了吗? 创建对象来检测的方法,会弹框提示。好烦,在找其他办法
    等这姑娘老在我心里:@我爱读书 好的 谢谢
    一线码农:@等这姑娘老在我心里 可以参考Github上的一个开源库,BabyBluetooth

本文标题:iOS检测蓝牙是否开启

本文链接:https://www.haomeiwen.com/subject/dwckkttx.html