iOS 蓝牙设备

iOS 蓝牙设备
一、 蓝牙管理类的初始化
CBCentralManager *blueManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil];
二、检查外部蓝牙设备是否可用
  • 在CBCentralManager初始化之后会自动调用centralManagerDidUpdateState,在蓝牙的状态为开即centralManager.state == CBManagerStatePoweredOn 时,进行蓝牙扫描。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    if (central.state == CBManagerStatePoweredOn) {
            [weakManager startScan];
        }
}
三、开始扫描蓝牙设备
  • 扫描指定UUID 的设备,在options 里面配置是否允许重复的key,当services入参为空值时,默认扫描所有设备。
    [self.blueManager scanForPeripheralsWithServices:@[] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];
四、结束蓝牙设备扫描
  • 在扫描完成蓝牙设备之后,要及时关闭蓝牙扫描
  [CBCentralManager stopScan];
四、连接指定设备
  1. 在正确扫描设备之后需要在CBCentralManagerDelegate实现其中的发现设备代理,用户可以在发现代理中进行数据的去重和回调操作。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
    if (central) {
        DeviceModel *model = [[DeviceModel alloc] init];
        model.deviceName = periphera
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值