连接到外设将触发(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
,因此如果BLE设备启用了配对,您将收到提示询问配对的提示。如果配对不成功,如果设备在配对失败后没有断开连接的命令,它将保持连接状态,但如果尝试发现其服务(*)和特性,则可能无法获得任何连接(取决于固件BLE设备的一侧已配置)。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@", peripheral);
[peripheral setDelegate:self];
[peripheral discoverServices:nil]; //* discover peripheral services
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (CBService *service in peripheral.services) {
NSLog(@"discovered service [%@]",service.UUID);
[peripheral discoverCharacteristics:nil forService:service];
}
}