iOS 蓝牙连接的流程
来源:互联网 发布:在线网络投资产品排行 编辑:程序博客网 时间:2024/06/05 05:14
iOS 蓝牙连接的流程:
一、在 .h 文件中
1、加入头文件 #import <CoreBluetooth/CoreBluetooth.h>
2、声明以下变量
@property (nonatomic, strong) CBCentralManager *m_manger; //管理者@property (nonatomic, strong) CBService *m_service; //服务@property (nonatomic, strong) CBPeripheral *m_peripheral; //外设
1、初始化管理者,扫描外设,连接指定外设
_m_manger = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_m_manger scanForPeripheralsWithServices:nil options:nil];
[self.m_manger connectPeripheral:peripheral options:description];
2、判断系统蓝牙是否开启
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
3、实现发现外设的回调
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
4、实现已连接外设的回调
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ _m_peripheral = peripheral; _m_peripheral.delegate = self; [_m_peripheral discoverServices:nil];}
5、实现发现服务的回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:nil forService:service]; }}
6、实现发现特征值的回调,并监听特征值
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if ([service.UUID isEqual:[UUIDTool serviceUUID01]]) { for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[UUIDTool characteristic01UUID]]) { } // 监听特征值 [_m_peripheral setNotifyValue:YES forCharacteristic:characteristic]; } }}
7、实现特征值改变的回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
8、实现发送指令反馈信息的回调
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ if (!error) { NSLog(@"发送指令成功"); } else{ NSLog(@"发送指令失败"); }}
9、实现外设连接失败的回调
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
10、实现外设连接后,再失去连接的回调
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
阅读全文
0 0
- iOS 蓝牙连接的流程
- 蓝牙连接外设的流程
- 02-iOS蓝牙连接流程介绍
- iOS蓝牙连接流程介绍-1
- [蓝牙]iOS蓝牙调用的一般流程
- [Android蓝牙]------连接蓝牙流程
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
- iOS开发—蓝牙4.0(BLE)与外设连接及收发数据的流程
- iOS蓝牙调用的一般流程
- iOS蓝牙调用的一般流程
- iOS蓝牙调用的一般流程
- iOS蓝牙开发流程
- iOS 蓝牙开发流程
- iOS 蓝牙 基本流程
- IOS Objective-c 蓝牙开发教程一蓝牙的连接
- 蓝牙连接参数关于IOS的限制
- Java中的split函数
- [web 前端] angular4之angular-io-datepicker
- Uart通信协议
- 宝宝秀项目学习(四)
- spring mvc
- iOS 蓝牙连接的流程
- 常用网址
- 名称空间
- c++ 中子类与父类构造析构的调用和顺序
- C# 方法的定义
- Ubuntu Server 16.04 安装MySQL
- JAVA mail邮箱发送信息
- Python IndentationError
- allegro学习之反馈电流走线宽度