当前位置: 首页 > 工具软件 > CoreBlueTooth > 使用案例 >

CoreBluetooth-外围模式

白赞
2023-12-01

这里只能介绍下外围模式开发的一般流程了,没经历过具体项目的洗礼,具体的坑点没接触到,以后如果有机会再加以补充。

一般流程模式

1.打开peripheralManager,设置peripheralManager的代理

2.创建characteristics,characteristics的properties 创建service,把characteristics添加到service中,再把service添加到peripheralManager中

3.开启广播advertising

4.对central的操作进行响应

4.1  读characteristics请求
4.2  写characteristics请求
4.3  订阅和取消订阅characteristics

代码流程

1.准备工作,引入头文件和代理

#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBPeripheralManagerDelegate>
@property (nonatomic,strong) CBPeripheralManager * perManager;
@end

2.打开peripheralManager,设置peripheralManager的代理

_perManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
/*
    会自动回调方法
     - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
只有在peripheral.state == CBManagerStatePoweredOn的状态下创建characteristics 和 service才有用
*/

3.创建characteristics,characteristics的description 创建service,把characteristics添加到service中,再把service添加到peripheralManager中

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{

    if (peripheral.state == CBManagerStatePoweredOn) {
        //服务 uuid换成自己的
        CBUUID * serviceUUID = [CBUUID UUIDWithString:@"1111"];
        CBMutableService * service = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];

        /*特征值 uuid换成自己的 
        properties也换成自己的,这里设置成可读、可写、广播
*/
        CBUUID * characteristicUUID = [CBUUID UUIDWithString:@"2222"];
        CBCharacteristic * characteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite|CBCharacteristicPropertyNotify) value:nil permissions:CBAttributePermissionsReadable];

        //特征值加入到服务
        service.characteristics = @[characteristic];

        /*注册服务 注册成功后的服务才能广播 注册后会自动回调方法 - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error; */

        [_perManager addService:service];
    }
}

4.开启广播advertising 。 调用方法- (void)startAdvertising:(nullable NSDictionary < NSString *, id > *)advertisementData;来开启广播,
参数可选:
CBAdvertisementDataLocalNameKey
CBAdvertisementDataTxPowerLevelKey
CBAdvertisementDataServiceUUIDsKey
CBAdvertisementDataServiceDataKey
CBAdvertisementDataManufacturerDataKey
CBAdvertisementDataOverflowServiceUUIDsKey
CBAdvertisementDataIsConnectable
CBAdvertisementDataSolicitedServiceUUIDsKey

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
    //开启广播服务
    [_perManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey:[CBUUID UUIDWithString:@"1111"]}];
}
//开启广播的回调
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error {

}

5.1 读characteristics请求

// 读数据请求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request {
    //请求的响应
    NSData *data = request.characteristic.value; //center想读取的数据
    [request setValue:data];
    [_perManager respondToRequest:request withResult:CBATTErrorSuccess];
}

5.2 写characteristics请求

//写数据请求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
    //请求的响应
    //需要转换成CBMutableCharacteristic对象才能进行写值
        CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic;
    c.value = request.value;
    [_perManager respondToRequest:[requests firstObject] withResult:CBATTErrorSuccess];
}

5.3 订阅和取消订阅characteristics

//订阅characteristics
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{
    NSLog(@"订阅了 %@的数据",characteristic.UUID);
    //每秒执行一次给主设备发送一个当前时间的秒数
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendData:) userInfo:characteristic  repeats:YES];
}

//取消订阅characteristics
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{
    NSLog(@"取消订阅 %@的数据",characteristic.UUID);
    //取消回应
    [timer invalidate];
}
 类似资料: