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

【CoreBluetooth】iOS 系统蓝牙框架的更多相关文章

  1. 苹果手机(ios系统)蓝牙BLE的一些特点

    摘自<BluetoothDesignGuidelines.pdf>文档 1. pairing: 苹果手机无法主动发起SMP配对流程,可通过以下两种方式发起配对流程: (1)从端主动发起配对 ...

  2. ios系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...

  3. IOS系统之蓝牙外接设备

    Ios系统对于蓝牙外接设备在iphone4以前都是蓝牙2.0的时候,需要通过苹果的审核,据统计通过率仅有2%左右,现在蓝牙2.0基本上处于淘汰状态,所以在这里就不考虑了. 现在iphone4s以后的设 ...

  4. iOS之蓝牙开发—CoreBluetooth详解

    CoreBluetooth的API是基于BLE4.0的标准的.这个框架涵盖了BLE标准的所有细节.仅仅只有新的iOS设备和Mac是和BLE标准兼容.在CoreBluetooth框架中,有两个主要的角色 ...

  5. IOS系统框架

    IOS系统框架:ios架构号MAC OS的基础框架相似:ios扮演底层硬件和应用程序的中介,底层为所有应用程序提供基础服务,高层则包含一些复杂巧妙的技术和服务,并且创建的应用程序不能直接访问硬件. C ...

  6. iOS 系统框架

    iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch l ...

  7. iOS 的基本框架

    在iOS中框架是一个目录,包含了共享资源库,用于访问该资源库中储存的代码的头文件,以及图像.声音文件等其他资源.共享资源库定义应用程序可以调用的函数和方法.    iOS为应用程序开发提供了许多可使用 ...

  8. 史上最全的常用iOS的第三方框架

    文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片 ...

  9. 常用iOS的第三方框架

    图像:1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...

随机推荐

  1. pikachu平台搭建

    1.将pikachu转移至htdocs 2.然后打开pikachu文件夹里的inc文件夹 3.里面对应的内容该成之前刚刚设置好的数据库服务器地址,用户名,密码和端口号 4.打开浏览器,输入http:/ ...

  2. Drf模块详细分析

    drf的请求模块 drf的request是在wdgi的request基础上再次封装 wsgi的request作为drf的request一个属性:_request 新的request对旧的request ...

  3. vue+vuex项目中怎么实现input模糊查询

    1,首先给input框添加方法,但是用的是element-ui的组件,对input进行了封装,不能直接用原生的方法!,在element组件中,input框中方法有实例参数$event,代表事件对象  ...

  4. js实现图片选中马上显示功能,选择后可以预览,即选即显

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. CPI和PPI,谁代表了通膨?

    CPI.PPI 一则旧闻,根据 2016年1月9日 统计局公布的数据,CPI同比增长1.6%,PPI同比下降5.9%. Consumer Price Index 缩写CPI,居民消费价格指数. 统计范 ...

  6. 避免重复数据查询DISTINCT

    [1]避免重复——DISTINCT SELECT  DISTINCT  job  FROM   t_enployee; #去除job字段的重复项,只显示所有job字段中的非重复项,比如job里有:te ...

  7. Platform device/driver注册过程

    Platform是一种虚拟总线,Platform机制将设备本身的资源注册进内核,有内核统一管理,在驱动程序使用这些资源时使用统一的接口,这样提高了程序的可移植性. Linux的大部分设备驱动都可以使用 ...

  8. 【使用python urllib时出现[SSL: CERTIFICATE_VERIFY_FAILED]报错的解决方案】

    "首先,这个报错是告诉你,你的证书有问题. 其次,出现这个问题的原因,在于Python本身. 问题原因 Python升级到2.7.9以后,引入了一个新特性. 当使用urllib打开https ...

  9. 如何用 Python 实现超级玛丽的人物行走和碰撞检测?

    ​ 功能介绍 人物行走 人物的行走速度这边分成水平方向(X轴)和竖直方向(Y轴),水平方向的速度要考虑加速度和摩擦力,竖直方向的速度要考虑重力加速度. 水平方向:设定X轴向右走的速度为大于0,向左走的 ...

  10. PHP 文件上传之如何识别文件伪装?——PHP的fileinfo扩展可!

    问题:文件上传时候需要验证上传的文件是否合法,文件伪装如何识别? 一个简单测试:把txt文件后缀直接改成jpg;上传 <!DOCTYPE html> <html> <ti ...