中心角色操作步骤
- 导入框架:在项目中导入Core Bluetooth框架。
- 初始化CBCentralManager:
- 创建
CBCentralManager
实例,通常在类的属性中定义。
- 实现
CBCentralManagerDelegate
代理协议。
- 使用
initWithDelegate:queue:options:
方法初始化CBCentralManager
,其中delegate
为当前类实例,queue
可以传入nil
使用主线程,options
一般为nil
。
- 关键代码示例:
@property (nonatomic, strong) CBCentralManager *centralManager;
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
- 等待中心管理器状态更新:实现
centralManagerDidUpdateState:
代理方法,检查中心管理器的状态,只有当状态为.poweredOn
时,才可以进行后续操作。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
// 可以开始扫描
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
}
- 扫描外设:调用
scanForPeripheralsWithServices:options:
方法扫描附近的蓝牙外设。services
参数可以传入nil
扫描所有设备,也可以传入特定的CBUUID
数组来扫描特定服务的设备。options
参数一般为nil
。
- 发现外设:实现
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
代理方法,在此方法中获取发现的外设信息。可以存储外设对象,以便后续连接。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"发现外设: %@", peripheral.name);
self.discoveredPeripheral = peripheral;
[self.centralManager stopScan];
[self.centralManager connectPeripheral:peripheral options:nil];
}
- 连接外设:调用
connectPeripheral:options:
方法连接特定的外设,options
参数一般为nil
。
- 处理连接结果:实现
centralManager:didConnectPeripheral:
和centralManager:didFailToConnectPeripheral:error:
代理方法,处理连接成功或失败的情况。
- 连接成功后,可以发现外设的服务和特征。
- 连接失败时,根据
error
信息处理错误。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"连接成功");
[peripheral discoverServices:nil];
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"连接失败: %@", error);
}
- 发现服务和特征:
- 连接成功后,调用外设的
discoverServices:
方法发现外设提供的服务,services
参数可以传入nil
发现所有服务,也可以传入特定的CBUUID
数组发现特定服务。
- 实现
peripheral:didDiscoverServices:
代理方法,在方法中遍历发现的服务,并对每个服务调用discoverCharacteristics:forService:
方法发现服务中的特征,characteristics
参数同样可以传入nil
或特定的CBUUID
数组。
- 实现
peripheral:didDiscoverCharacteristicsForService:error:
代理方法处理发现特征的结果。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"发现特征: %@", characteristic);
}
}
- 数据交互:根据特征的属性(如可读、可写等)进行数据读写操作。
- 读取数据:调用
readValueForCharacteristic:
方法读取特征的值,并实现peripheral:didUpdateValueForCharacteristic:error:
代理方法获取读取到的数据。
- 写入数据:调用
writeValue:forCharacteristic:type:
方法写入数据到特征,type
参数根据特征的属性选择合适的写入类型。如果特征支持通知,还可以调用setNotifyValue:forCharacteristic:
方法开启通知,实现peripheral:didUpdateNotificationStateForCharacteristic:error:
和peripheral:didUpdateValueForCharacteristic:error:
代理方法处理通知相关操作。
- 断开连接:调用
cancelPeripheralConnection:
方法断开与外设的连接,并实现centralManager:didDisconnectPeripheral:error:
代理方法处理断开连接的情况。
外设角色操作步骤
- 导入框架:同样在项目中导入Core Bluetooth框架。
- 初始化CBPeripheralManager:
- 创建
CBPeripheralManager
实例,定义在类属性中。
- 实现
CBPeripheralManagerDelegate
代理协议。
- 使用
initWithDelegate:queue:options:
方法初始化CBPeripheralManager
,delegate
为当前类实例,queue
可以为nil
使用主线程,options
一般为nil
。
- 关键代码示例:
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
- 等待外设管理器状态更新:实现
peripheralManagerDidUpdateState:
代理方法,检查外设管理器的状态,只有当状态为.poweredOn
时,才可以进行后续操作。
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
// 可以开始发布服务等操作
}
}
- 创建服务和特征:
- 使用
CBService
类创建服务对象,使用CBCharacteristic
类创建特征对象。
- 例如:
CBUUID *serviceUUID = [CBUUID UUIDWithString:@"YOUR_SERVICE_UUID"];
CBService *myService = [[CBService alloc] initWithType:serviceUUID primary:YES];
CBUUID *characteristicUUID = [CBUUID UUIDWithString:@"YOUR_CHARACTERISTIC_UUID"];
CBCharacteristic *myCharacteristic = [[CBCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable];
- 添加服务到外设管理器:将创建好的服务添加到外设管理器,调用
addService:
方法。
[self.peripheralManager addService:myService];
- 处理连接请求:实现
peripheralManager:didReceiveReadRequest:
和peripheralManager:didReceiveWriteRequests:
代理方法,处理中心设备的读写请求。
- 更新特征值:如果需要主动更新特征值,调用
updateValue:forCharacteristic:onSubscribedCentrals:
方法通知订阅的中心设备特征值的变化。
关键类
- CBCentralManager:用于管理中心角色的蓝牙操作,包括扫描、连接外设等。
- CBPeripheral:代表一个被发现的蓝牙外设,通过它可以发现服务、特征,并进行数据交互。
- CBService:表示蓝牙设备提供的一个服务,一个外设可以有多个服务。
- CBCharacteristic:表示服务中的一个特征,数据的读写操作主要针对特征进行。
- CBPeripheralManager:用于管理外设角色的蓝牙操作,包括发布服务、处理连接请求等。
关键方法(已在上述步骤中详细提及部分不再重复)
- CBCentralManager的方法:
scanForPeripheralsWithServices:options:
:扫描附近的蓝牙外设。
connectPeripheral:options:
:连接特定的蓝牙外设。
cancelPeripheralConnection:
:断开与蓝牙外设的连接。
- CBPeripheral的方法:
discoverServices:
:发现外设提供的服务。
discoverCharacteristics:forService:
:发现服务中的特征。
readValueForCharacteristic:
:读取特征的值。
writeValue:forCharacteristic:type:
:写入数据到特征。
setNotifyValue:forCharacteristic:
:开启或关闭特征的通知。
- CBPeripheralManager的方法:
addService:
:将服务添加到外设管理器。
updateValue:forCharacteristic:onSubscribedCentrals:
:更新特征值并通知订阅的中心设备。