暂时 第一次功能性研究,具体实现,后续添加;
系统分类
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 *)dict {
NSLog(@"%@",dict);
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)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 *)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