https://www.jianshu.com/p/eb58dcbae5f9

2016.06.07 23:04* 字数 285 阅读 852评论 4喜欢 3

暂时 第一次功能性研究,具体实现,后续添加;

系统分类

iOS 设备,同一时间,只能处于某一种状态:作为中心设备,或者作为周边设备;

一般情况:iOS设备链接智能硬件,使用时作为中心设备,与硬件交互;

玩家对战,当面传输:一个作为中心,一个作为周边设备;

CBCentralManager - 中心设备管理,用于搜索周边设备,对应CBPeripheral使用

CBPeripheralManager - 周边设备管理,用于作为周边设备,对应CBCentral使用

CBPeripheral - 周边设备

CBCentral - 中心设备

CBService - 设备 服务

CBCharacteristic - 设备 服务 特征

CBDescriptor - 设备 服务 特征 描述

CBError - 错误

CBUUID - 唯一码

CBAdvertisementData -

CBATTRequest -

CBCentralManager 中心管理

  • 1 初始化 扫描周边设备

// 初始化 manager

self.centerManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

// [self.centerManager stopScan];  可以停止扫描

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

if (central.state == CBCentralManagerStatePoweredOn) {

NSLog(@"蓝牙 - 打开");

// 开始扫描,周边设备

[self.centerManager scanForPeripheralsWithServices:nil options:nil];

} else {

NSLog(@"蓝牙 异常,其他状态自行判断");

}

}

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict {

NSLog(@"%@",dict);

}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {

if (peripheral.name) {

NSLog(@"扫描到设备 %@",peripheral.name);

}

}

  • 2 连接设备

// 某处,调用链接设备

[self.centerManager connectPeripheral:currentPer options:nil];

currentPer.delegate = self;

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

NSLog(@"链接成功");

}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {

NSLog(@"链接失败");

}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {

NSLog(@"设备断开链接");

}

CBPeripheral 设备信息

属性

delegate:代理

name:设备名称

RSSI:设备信号强度

state:设备链接状态

services:设备提供的服务

下面,方法都对应了代理

  • 扫描 设备的某些 UUID 的服务

[currentPer discoverServices:@[@"UUID"]];

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {

NSLog(@"%@",service.UUID);

}

  • 扫描 设备的某服务的 UUID 服务?

[currentPer discoverIncludedServices:@[] forService:service];

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error {

NSLog(@"%@",service.UUID);

}

  • 扫描 设备的某个服务中的 UUID 特性

[peripheral discoverCharacteristics:@[] forService:peripheral.services.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {

NSLog(@"%@",service.characteristics);

}

  • 扫描 设备的某个特征 UUID

[peripheral discoverDescriptorsForCharacteristic:peripheral.services.lastObject.characteristics.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {

NSLog(@"%@",characteristic);

}

  • 获取设备 蓝牙信号强度

[peripheral readRSSI];

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {

NSLog(@"%@",RSSI.stringValue);

}

  • 读取 特征

[peripheral readValueForCharacteristic:peripheral.services.lastObject.characteristics.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

NSLog(@"%@",characteristic);

}

  • 读取 描述

[peripheral readValueForDescriptor:peripheral.services.lastObject.characteristics.lastObject.descriptors.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {

NSLog(@"%@",descriptor);

}

  • 添加 监听

[peripheral setNotifyValue:YES forCharacteristic:peripheral.services.lastObject.characteristics.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {

NSLog(@"%@",characteristic);

}

  • 写入描述

[peripheral writeValue:[NSData data] forDescriptor:peripheral.services.lastObject.characteristics.lastObject.descriptors.lastObject];

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {

NSLog(@"%@",descriptor);

}

  • 写入 特征

[peripheral writeValue:[NSData data] forCharacteristic:peripheral.services.lastObject.characteristics.lastObject type:CBCharacteristicWriteWithResponse];

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {

NSLog(@"%@",characteristic);

}

其他

// 最大数据量?

NSLog(@"%zi",[peripheral maximumWriteValueLengthForType:CBCharacteristicWriteWithResponse]);

// 修改 名称

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral {

NSLog(@"%@",peripheral);

}

// 修改 服务

- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices {

NSLog(@"%@",peripheral);

}

// 修改 RSSI

- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error {

NSLog(@"%@",peripheral.RSSI.stringValue);

}

CBService CBMutableService 服务

作为中心获取,作为周边创建

// 服务 包含 的 服务

for (CBService *ser in service.includedServices) {

NSLog(@"%@",ser.UUID);

}

// 服务 包含特征

for (CBCharacteristic *charcter in service.characteristics) {

NSLog(@"%@",charcter.UUID);

}

CBCharacteristic CBMutableCharacteristic 特征

作为中心获取,作为周边创建

CBCharacteristicProperties property = characteristic.properties;// 读写等属性

NSData *data = characteristic.value;// 特征数据

// 特征 包含的描述

for (CBDescriptor *desc in characteristic.descriptors) {

NSLog(@"%@",desc.UUID);

}

CBDescriptor CBMutableDescriptor

作为中心获取,作为周边创建

NSLog(@"%@",descriptor.value);

1

最新文章

  1. 2015-12-21(box-sizing:border-box)
  2. BI cube的前世今生:商业智能BI为什么需要cube技术
  3. [SVN] SVN在Eclipse里的各个状态解释
  4. 使用flume的一个例子
  5. Android -- 自定义View小Demo,关于Rect绘制Android机器人(一)
  6. scala 隐式转换
  7. [Unity菜鸟] FBX模型动画提取
  8. 使用Google Chart API绘制组合图
  9. 数据库性能优化一:SQL索引一步到位
  10. 一个用UpdateLayeredWindow实现窗体半透明的delphi的代码
  11. Delphi下获取IE的UserAgent的方法
  12. 为什么用户主目录下.bash_profile没有自动执行
  13. sql语句:创建事物
  14. 背景图height:100%显示
  15. Mac 性能测试环境搭建——理论篇
  16. BMC手册 — 第一模块 BMC介绍
  17. EntityFramework6之原生SQL
  18. ORACLE——RMAN 参数解读
  19. Chrome插件开发,美化网页上的文件列表。chrome-extension,content-scripts
  20. java算法----排序----(7)堆排序

热门文章

  1. 【想见你】剧情解析byZlc
  2. Refusing to install package with name &quot;webpack&quot; under a package
  3. html标签的快捷
  4. Ansible - iventory
  5. tomcat使用中的笔记
  6. inconsistent use of tabs and spaces in indentation
  7. 熟悉这几道 Redis 高频面试题,面试不用愁
  8. PTA的Python练习题(十六)
  9. Java方法的定义和使用
  10. html5的canvas2