- 初始化中心设备
- 重要方法:通过
CBCentralManager
类来初始化中心设备。例如:
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
- **重要代理**:`CBCentralManagerDelegate`。该代理中有多个重要方法,如`centralManagerDidUpdateState:`用于检测中心设备的状态更新。当中心设备状态发生变化时会调用此方法,我们可以在此方法中判断蓝牙是否可用。示例代码如下:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStatePoweredOn:
NSLog(@"蓝牙已打开,可进行扫描");
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
break;
case CBCentralManagerStatePoweredOff:
NSLog(@"蓝牙已关闭");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"蓝牙未授权");
break;
case CBCentralManagerStateUnknown:
NSLog(@"蓝牙状态未知");
break;
case CBCentralManagerStateUnsupported:
NSLog(@"设备不支持蓝牙");
break;
}
}
- 扫描外设
- 重要方法:在
CBCentralManager
类中使用scanForPeripheralsWithServices:options:
方法进行扫描。例如:
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
- **重要代理**:`CBCentralManagerDelegate`中的`centralManager:didDiscoverPeripheral:advertisementData:RSSI:`方法。当扫描到蓝牙外设时,系统会调用此方法。在此方法中可以获取到发现的外设`CBPeripheral`对象,以及外设的广播数据`advertisementData`和信号强度`RSSI`等信息。示例代码如下:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"发现外设:%@,信号强度:%@", peripheral.name, RSSI);
self.discoveredPeripheral = peripheral;
[central stopScan];
[central connectPeripheral:peripheral options:nil];
}
- 连接外设
- 重要方法:在
CBCentralManager
类中调用connectPeripheral:options:
方法连接外设。例如:
[self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];
- **重要代理**:
- `CBCentralManagerDelegate`中的`centralManager:didConnectPeripheral:`方法,当成功连接到外设时会调用此方法。在此方法中可以进一步配置外设,例如发现服务和特征等。示例代码如下:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"已连接到外设:%@", peripheral.name);
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
- `CBCentralManagerDelegate`中的`centralManager:didFailToConnectPeripheral:error:`方法,当连接外设失败时会调用此方法,可以在此方法中处理连接失败的情况,例如提示用户或进行重试等。示例代码如下:
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"连接外设失败:%@", error);
}
- `CBPeripheralDelegate`中的`peripheral:didDiscoverServices:error:`方法,当发现外设的服务时会调用此方法,可在此方法中进一步发现服务中的特征。示例代码如下:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services) {
NSLog(@"发现服务:%@", service.UUID);
[peripheral discoverCharacteristics:nil forService:service];
}
}
- `CBPeripheralDelegate`中的`peripheral:didDiscoverCharacteristicsForService:error:`方法,当发现服务中的特征时会调用此方法,之后就可以根据需求对特征进行读写等操作。示例代码如下:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"发现特征:%@", characteristic.UUID);
if (characteristic.properties & CBCharacteristicPropertyRead) {
[peripheral readValueForCharacteristic:characteristic];
}
}
}