|
|
|
@ -282,22 +282,11 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
|
|
|
|
// 处理传入的UUID参数 |
|
|
|
for uuidStr in uuids { |
|
|
|
do { |
|
|
|
let cbuuid: CBUUID |
|
|
|
if uuidStr.count == 4 { |
|
|
|
// 短UUID,直接使用 |
|
|
|
cbuuid = CBUUID(string: uuidStr) |
|
|
|
} else if uuidStr.count == 36 { |
|
|
|
// 完整UUID |
|
|
|
cbuuid = CBUUID(string: uuidStr) |
|
|
|
} else { |
|
|
|
os_log("⚠️ 无效的UUID格式: %{public}@", log: logger, type: .default, uuidStr) |
|
|
|
continue |
|
|
|
} |
|
|
|
if let cbuuid = parseCBUUID(from: uuidStr) { |
|
|
|
serviceUUIDs.append(cbuuid) |
|
|
|
os_log("✅ 添加扫描过滤器UUID: %{public}@", log: logger, type: .info, uuidStr) |
|
|
|
} catch { |
|
|
|
os_log("❌ 构造UUID失败: %{public}@, 错误: %{public}@", log: logger, type: .error, uuidStr, error.localizedDescription) |
|
|
|
} else { |
|
|
|
os_log("⚠️ 无效的UUID格式或字符: %{public}@", log: logger, type: .error, uuidStr) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -319,6 +308,40 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
} |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
/// 验证并解析 UUID 字符串为 CBUUID |
|
|
|
/// - Parameter uuidStr: UUID 字符串(支持 4 位短 UUID、36 位完整 UUID;扩展支持 32 位不带短横线 UUID) |
|
|
|
/// - Returns: 解析成功返回 CBUUID,否则返回 nil |
|
|
|
private func parseCBUUID(from uuidStr: String) -> CBUUID? { |
|
|
|
let trimmed = uuidStr.trimmingCharacters(in: .whitespacesAndNewlines) |
|
|
|
|
|
|
|
// 4 位短 UUID:仅允许十六进制字符,通过正则校验 |
|
|
|
if trimmed.count == 4 { |
|
|
|
if trimmed.range(of: "^[0-9a-fA-F]{4}$", options: .regularExpression) != nil { |
|
|
|
return CBUUID(string: trimmed.lowercased()) |
|
|
|
} else { |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 36 位完整 UUID:使用系统 UUID 进行格式校验 |
|
|
|
if trimmed.count == 36, let nsuuid = UUID(uuidString: trimmed) { |
|
|
|
return CBUUID(nsuuid: nsuuid) |
|
|
|
} |
|
|
|
|
|
|
|
// 32 位完整 UUID(不带短横线):按 8-4-4-4-12 格式插入短横线后校验 |
|
|
|
if trimmed.count == 32, |
|
|
|
trimmed.range(of: "^[0-9a-fA-F]{32}$", options: .regularExpression) != nil { |
|
|
|
let lower = trimmed.lowercased() |
|
|
|
let formatted = "\(lower.prefix(8))-\(lower.dropFirst(8).prefix(4))-\(lower.dropFirst(12).prefix(4))-\(lower.dropFirst(16).prefix(4))-\(lower.dropFirst(20))" |
|
|
|
if let nsuuid = UUID(uuidString: formatted) { |
|
|
|
return CBUUID(nsuuid: nsuuid) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 其他长度不支持 |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
/// 停止扫描 |
|
|
|
public func stopScan() { |
|
|
|
|