|
|
|
@ -229,11 +229,14 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
|
|
|
|
isScanning = true |
|
|
|
|
|
|
|
// 移除服务UUID过滤,扫描所有设备 |
|
|
|
// // 移除服务UUID过滤,扫描所有设备 |
|
|
|
// manager.scanForPeripherals(withServices: nil, options: [ |
|
|
|
// CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据 |
|
|
|
// ]) |
|
|
|
//let serviceUUIDs = [BleConst.SERVICE_UUID, BleConst.SERVICE_UUID1] |
|
|
|
manager.scanForPeripherals(withServices: nil, options: [ |
|
|
|
CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据 |
|
|
|
CBCentralManagerScanOptionAllowDuplicatesKey: true |
|
|
|
]) |
|
|
|
|
|
|
|
os_log("🔍 开始扫描所有BLE设备(无过滤),超时时间: %.1f秒", log: logger, type: .info, timeout) |
|
|
|
|
|
|
|
// 设置扫描超时 |
|
|
|
@ -383,6 +386,7 @@ private var cmdReplyType: UInt8 = 0 |
|
|
|
|
|
|
|
// 遍历所有已发现的设备,查找匹配的设备名 |
|
|
|
for (deviceId, deviceInfo) in discoveredDevices { |
|
|
|
print("deviceInfo: \(deviceInfo)") |
|
|
|
if let name = deviceInfo["name"] as? String, name == deviceName { |
|
|
|
// 获取对应的广播数据 |
|
|
|
var deviceData = deviceInfo |
|
|
|
@ -1216,14 +1220,76 @@ extension BleService: CBCentralManagerDelegate { |
|
|
|
os_log("📱 设备RSSI变化较大 - 名称: %{public}@, UUID: %{public}@, RSSI: %d → %d", log: logger, type: .debug, deviceName, deviceId, oldRssi, newRssi) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 新设备,需要上报 |
|
|
|
// 新设备,需要上报并打印详细信息 |
|
|
|
shouldNotify = true |
|
|
|
os_log("📱 发现新设备 - 名称: %{public}@, UUID: %{public}@", log: logger, type: .info, deviceName, deviceId) |
|
|
|
|
|
|
|
// 打印 hex 数据 - 直接从 deviceInfo 获取 |
|
|
|
if let hex = deviceInfo["hex"] as? String { |
|
|
|
os_log("📡 设备 hex 数据: %{public}@", log: logger, type: .info, hex) |
|
|
|
// 打印详细的设备信息 |
|
|
|
os_log("📱 ===== 发现新BLE设备 =====", log: logger, type: .info) |
|
|
|
os_log("📱 设备名称: %{public}@", log: logger, type: .info, deviceName) |
|
|
|
os_log("📱 设备UUID(MAC地址): %{public}@", log: logger, type: .info, deviceId) |
|
|
|
os_log("📱 RSSI信号强度: %d dBm", log: logger, type: .info, RSSI.intValue) |
|
|
|
os_log("📱 发现时间: %{public}@", log: logger, type: .info, Date().description) |
|
|
|
|
|
|
|
// 打印服务UUID信息 |
|
|
|
if let serviceUUIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID] { |
|
|
|
var uuidDetails: [String] = [] |
|
|
|
for uuid in serviceUUIDs { |
|
|
|
let uuidString = uuid.uuidString |
|
|
|
// 检查是否为16位UUID(通常显示为4个字符) |
|
|
|
if uuidString.count == 4 { |
|
|
|
let fullUUID = "0000\(uuidString)-0000-1000-8000-00805F9B34FB" |
|
|
|
uuidDetails.append("\(uuidString) (完整: \(fullUUID))") |
|
|
|
} else { |
|
|
|
uuidDetails.append(uuidString) |
|
|
|
} |
|
|
|
} |
|
|
|
os_log("📱 服务UUIDs: %{public}@", log: logger, type: .info, uuidDetails.joined(separator: ", ")) |
|
|
|
os_log("📱 服务UUID数量: %d", log: logger, type: .info, serviceUUIDs.count) |
|
|
|
} else { |
|
|
|
os_log("📱 服务UUIDs: 无", log: logger, type: .info) |
|
|
|
} |
|
|
|
|
|
|
|
// 打印制造商数据 |
|
|
|
if let manufacturerData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data { |
|
|
|
let hexString = manufacturerData.map { String(format: "%02X", $0) }.joined(separator: " ") |
|
|
|
os_log("📱 制造商数据: %{public}@", log: logger, type: .info, hexString) |
|
|
|
} else { |
|
|
|
os_log("📱 制造商数据: 无", log: logger, type: .info) |
|
|
|
} |
|
|
|
|
|
|
|
// 打印服务数据 |
|
|
|
if let serviceData = advertisementData[CBAdvertisementDataServiceDataKey] as? [CBUUID: Data] { |
|
|
|
for (serviceUUID, data) in serviceData { |
|
|
|
let hexString = data.map { String(format: "%02X", $0) }.joined(separator: " ") |
|
|
|
os_log("📱 服务数据[%{public}@]: %{public}@", log: logger, type: .info, serviceUUID.uuidString, hexString) |
|
|
|
} |
|
|
|
} else { |
|
|
|
os_log("📱 服务数据: 无", log: logger, type: .info) |
|
|
|
} |
|
|
|
|
|
|
|
// 打印本地名称 |
|
|
|
if let localName = advertisementData[CBAdvertisementDataLocalNameKey] as? String { |
|
|
|
os_log("📱 本地名称: %{public}@", log: logger, type: .info, localName) |
|
|
|
} |
|
|
|
|
|
|
|
// 打印TX功率等级 |
|
|
|
if let txPowerLevel = advertisementData[CBAdvertisementDataTxPowerLevelKey] as? NSNumber { |
|
|
|
os_log("📱 TX功率等级: %d dBm", log: logger, type: .info, txPowerLevel.intValue) |
|
|
|
} |
|
|
|
|
|
|
|
// 打印是否可连接 |
|
|
|
if let isConnectable = advertisementData[CBAdvertisementDataIsConnectable] as? NSNumber { |
|
|
|
os_log("📱 可连接状态: %{public}@", log: logger, type: .info, isConnectable.boolValue ? "是" : "否") |
|
|
|
} |
|
|
|
|
|
|
|
// 打印完整的原始广播数据(十六进制) |
|
|
|
if let hex = deviceInfo["hex"] as? String, !hex.isEmpty { |
|
|
|
os_log("📱 原始广播数据(HEX): %{public}@", log: logger, type: .info, hex) |
|
|
|
} else { |
|
|
|
os_log("📱 原始广播数据(HEX): 无", log: logger, type: .info) |
|
|
|
} |
|
|
|
|
|
|
|
os_log("📱 =========================", log: logger, type: .info) |
|
|
|
} |
|
|
|
|
|
|
|
// 更新缓存(无论是否上报都要更新) |
|
|
|
|