美文网首页iOS开发- 蓝牙相关
【CoreBluetooth】iOS 系统蓝牙框架

【CoreBluetooth】iOS 系统蓝牙框架

作者: 居然是村长 | 来源:发表于2016-06-07 23:04 被阅读788次

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

系统分类

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

相关文章

  • iOS 蓝牙特技

    常用iOS 蓝牙 系统框架:CoreBlueTooth 简介: 可用于第三方蓝牙设备交互,设备必须支持蓝牙4.0 ...

  • iOS 蓝牙开发笔记

    iOS蓝牙框架介绍 (CoreBluetooth介绍) CoreBluetooth中涉及以下对象类: CBCent...

  • iOS 关于蓝牙开发

    蓝牙库: 当前iOS中的蓝牙开发使用的都是系统自带的蓝牙库

  • iOS 蓝牙交互基础知识

    要在iOS中集成蓝牙功能,需要使用的是CoreBluetooth框架,通过CoreBluetooth框架提供的AP...

  • iOS CoreBluetooth

    CoreBluetooth 在iOS和Mac应用中,CoreBluetooth框架用来与BLE(低功耗蓝牙)设备通...

  • iOS 蓝牙

    1.蓝牙的基础知识 1. iOS中开发蓝牙常用的系统库是

  • 【CoreBluetooth】iOS 系统蓝牙框架

    暂时 第一次功能性研究,具体实现,后续添加; 系统分类 iOS 设备,同一时间,只能处于某一种状态:作为中心设备,...

  • iOS蓝牙编程CoreBluetooth

    iOS的蓝牙框架是支持蓝牙4.0协议的。理解iOS CoreBluetooth两个很重要的概念,Central 和...

  • iOS蓝牙4.0,收发数据设计

    iOS蓝牙开发,现在常规使用的是CoreBlueTooth.framework,即蓝牙4.0开发框架。 1.CBC...

  • 蓝牙开发

    蓝牙开发使用CoreBluetooth框架实现两个iOS设备、iOS设备与非iOS蓝牙设备的交互。要注意的一点是目...

网友评论

    本文标题:【CoreBluetooth】iOS 系统蓝牙框架

    本文链接:https://www.haomeiwen.com/subject/krzddttx.html