/*
建立中心设备
扫描外设(Discover Peripheral)
连接外设(Connect Peripheral)
扫描外设中的服务和特征(Discover Services And Charateristics)
利用特征与外设做数据交互(Explore And Interact)
断开连接(Disconnect)
*/ #import "ViewController.h"
#import <coreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate> @property (nonatomic, strong) CBCentralManager *mgr;
/** 发现的外设数组*/
@property (nonatomic, strong) NSMutableArray *peripheralArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 1.建立中心设备
// queque:如果说你传空,在主队列
self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; // 2.扫描外设 } - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; // 最后一步,断开连接
[self.mgr stopScan];
} #pragma CBCentralManagerDelegate
/*
state:
CBManagerStateUnknown = 0, 未知
CBManagerStateResetting, 重置
CBManagerStateUnsupported, 不支持
CBManagerStateUnauthorized, 未经授权
CBManagerStatePoweredOff, 没有启动
CBManagerStatePoweredOn, 开启
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSLog(@"state = %zd", central.state);
// 蓝牙开启
if (central.state == CBManagerStateUnknown) { NSLog(@"未知的蓝牙");
}
if (central.state == CBManagerStatePoweredOn) { // 2.扫描外设
[self.mgr scanForPeripheralsWithServices:nil options:nil];
}
} /**
3.连接外设(Connect Peripheral)
@param peripheral 外设
@param advertisementData 相关的二进制数据
@param RSSI 信号强度
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI { // 1.记录外设,必须要有数组进来行存储
if ([self.peripheralArray containsObject:peripheral]) { [self.peripheralArray addObject:peripheral];
}
// 2.隐藏步骤,tableview表格列表(省略了)
// 3.连接外围设备
[self.mgr connectPeripheral:peripheral options:nil];
// 4.设置外设的代理
peripheral.delegate = self; } // 连接到外设之后,会调用这个代理方法
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 扫描服务
// 传nil代表扫描所有服务
[peripheral discoverServices:nil]; } #pragma CBPeripheralDelegate
// 外设扫描里面的服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { // 获取外设的服务
for (CBService *service in peripheral.services) { if ([service.UUID.UUIDString isEqualToString:@""]) {
// uuid一致,那就开始扫描特征
[peripheral discoverCharacteristics:nil forService:service]; }
}
} // 当发现到特征的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { //
for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID.UUIDString isEqualToString:@""]) { // [peripheral readValueForDescriptor:<#(nonnull CBDescriptor *)#>]; // [peripheral writeValue:<#(nonnull NSData *)#> forDescriptor:<#(nonnull CBDescriptor *)#>];
}
}
} - (NSMutableArray *)peripheralArray { if (!_peripheralArray) { _peripheralArray = [NSMutableArray array];
}
return _peripheralArray;
}

最新文章

  1. ios培训机构排名
  2. BZOJ 2120 数颜色&amp;2453 维护队列 [带修改的莫队算法]【学习笔记】
  3. iOS通知
  4. iOS阶段学习第14天笔记(NSString与NSMutableString)
  5. css样式—字体垂直、水平居中
  6. ruby 资源收集
  7. using
  8. UE4简单AI
  9. 51nod 1413 权势二进制 背包dp
  10. CSS中的视觉格式化模型
  11. 如何在VS C++中高亮用户自定义关键字
  12. VS2015 C#.net4.6 windows的定时服务
  13. HDU 4294 Multiple(搜索+数学)
  14. ESXi5.0误删除虚拟机还有办法恢复吗?答案是可以!
  15. 计算机17-3,4作业E
  16. 游记-HNOI2019
  17. Android 软件退出系统方法重写
  18. 学习笔记43—Linux基础集
  19. 课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 0、学习目标
  20. Vue + Element UI 实现权限管理系统 前端篇(五):国际化实现

热门文章

  1. SystemLogParser ArcGIS Server运维工程师的好助手
  2. 对抗网络GAN的应用实例
  3. TensorFlow分布式部署【多机多卡】
  4. Dialog向Activity传递数据
  5. Android自带语音播报+讯飞语音播报封装(直接用)
  6. Unity Optimization UNITY优化关注列表
  7. centos7 安装mariadb最新版并配置
  8. 【Java】数组使用
  9. NSArray排序方法讲解
  10. 如何创建一个Quartz.NET的工作,需要注射autofac