MST

星途 面试题库

面试题:Objective-C中使用Core Bluetooth连接外设的主要步骤

请详细描述在Objective-C中,运用Core Bluetooth框架连接一个蓝牙外设的主要流程,包括初始化中心设备、扫描外设、连接外设等关键步骤,并指出每个步骤可能涉及到的重要方法和代理。
24.4万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试
  1. 初始化中心设备
    • 重要方法:通过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;
    }
}
  1. 扫描外设
    • 重要方法:在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];
}
  1. 连接外设
    • 重要方法:在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];
        }
    }
}