Browse Source

修复Ios

newdev_shunjiawei
fdp 1 year ago
parent
commit
8f5fc7c131
  1. 46
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

46
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

@ -249,7 +249,10 @@ private var cmdReplyType: UInt8 = 0
clearPairedMacAddress()
}
/// 开始扫描BLE设备
public func startScan(timeout: TimeInterval = 30.0) -> Bool {
/// @param timeout 扫描超时时间,默认30秒
/// @param uuids 要扫描的服务UUID列表
/// @return 是否成功开始扫描
public func startScan(timeout: TimeInterval = 30.0, uuids: [String]) -> Bool {
if centralManager == nil {
initializeCentralManager()
}
@ -269,16 +272,41 @@ private var cmdReplyType: UInt8 = 0
isScanning = true
// // 移除服务UUID过滤,扫描所有设备
// manager.scanForPeripherals(withServices: nil, options: [
// CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据
// ])
let serviceUUIDs = [BleConst.SERVICE_UUID, BleConst.SERVICE_UUID1]
manager.scanForPeripherals(withServices: nil, options: [
// 构建服务UUID数组进行过滤
var serviceUUIDs: [CBUUID] = []
// // 添加默认的服务UUID
// serviceUUIDs.append(BleConst.SERVICE_UUID)
// serviceUUIDs.append(BleConst.SERVICE_UUID1)
// 处理传入的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: .warning, uuidStr)
continue
}
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)
}
}
// 开始扫描,使用构建的服务UUID数组进行过滤
manager.scanForPeripherals(withServices: serviceUUIDs.isEmpty ? nil : serviceUUIDs, options: [
CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据
])
os_log("🔍 开始扫描所有BLE设备(无过滤),超时时间: %.1f秒", log: logger, type: .info, timeout)
let filterInfo = serviceUUIDs.isEmpty ? "无过滤" : "过滤器数量: \(serviceUUIDs.count)"
os_log("🔍 开始扫描BLE设备(%{public}@),超时时间: %.1f秒", log: logger, type: .info, filterInfo, timeout)
// 设置扫描超时
scanTimer?.invalidate()

Loading…
Cancel
Save