【CoreBluetooth】iOS 系统蓝牙框架

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

转载于:https://www.cnblogs.com/sundaysgarden/p/10900570.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开发一个iOS系统蓝牙串口助手可以通过以下步骤实现: 1. 创建新的iOS项目:在Xcode中创建一个新的iOS项目。 2. 导入CoreBluetooth框架:在项目设置中导入CoreBluetooth框架,以便在代码中使用蓝牙相关的功能。 3. 设置蓝牙权限:在Info.plist文件中添加NSBluetoothAlwaysUsageDescription和NSBluetoothPeripheralUsageDescription键值对,并提供相应的描述信息,以获取蓝牙权限。 4. 初始化CBCentralManager:在应用程序的适当位置创建CBCentralManager对象,用于管理蓝牙设备的扫描和连接。 5. 实现CBCentralManagerDelegate:作为CBCentralManager的代理,实现相应的代理方法来处理蓝牙设备的扫描和连接状态变化。 6. 扫描蓝牙设备:使用CBCentralManager开始扫描周围的蓝牙设备,并在代理方法中获取扫描到的设备信息。 7. 连接蓝牙设备:选择要连接的蓝牙设备,使用CBCentralManager连接到目标设备,并在连接成功或失败时得到相应的回调。 8. 发送和接收数据:通过CBPeripheral对象与连接的蓝牙设备进行数据交互,可以发送数据到设备或从设备接收数据。 9. 处理数据:根据需要对接收到的数据进行解析和处理。可以将接收到的数据显示在界面上或执行其他操作。 10. 断开连接:当需要断开蓝牙设备连接时,使用CBCentralManager断开连接,并释放相关资源。 以上是一个简要的步骤,可以根据具体需求和功能进行进一步的开发和优化。需要注意的是,蓝牙通信涉及到一些技术细节和协议,需要对蓝牙相关知识有一定的了解和掌握。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值