diff --git a/HDFwear/Tools/BluetoothManager+Function.swift b/HDFwear/Tools/BluetoothManager+Function.swift index aca81ff..8cb5c11 100644 --- a/HDFwear/Tools/BluetoothManager+Function.swift +++ b/HDFwear/Tools/BluetoothManager+Function.swift @@ -126,4 +126,122 @@ extension BluetoothManager { self.setCmdClosure = completion sendData(data) } + + //MARK: - 发送数据 + func sendData(_ data: Data) { + if peripheral == nil { + print("peripheral == nil") + return + } + + var serverUUID: String = "" + var writeUUID: String = "" + switch platform { + case ._828: + serverUUID = BLEConfig.ServerUUID + writeUUID = BLEConfig.WriteUUID + default: + break + } + + if let services = peripheral?.services { + for service in services { + if service.uuid.uuidString == serverUUID { + if service.characteristics == nil { + return + } + for char in service.characteristics! { + if char.uuid.uuidString == writeUUID { + if platform == ._828 { + if data.count <= 20 { + print("发送: \([UInt8](data))") + peripheral?.writeValue(data, for: char, type: .withoutResponse) + } else { + print("发送 拼: \([UInt8](data))") + let n = data.count/20 + (data.count%20 == 0 ? 0 : 1) + for i in 0.. 12 else { return } + let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) + let data = Data(bytes: bytes, count: bytes.count) + switch msgId { + case 0x8001: + parseDefaultResponseData(bytes) + case 0x8002:// 设备信息查询应答 + print("设备信息查询应答") + case 0x8009:// 实时步数、卡路里、距离自动上报 + print("实时步数、卡路里、距离自动上报") + case 0x8010:// 电量变化自动上报 + print("电量变化自动上报") + case 0x8014:// 电话挂断 + print("电话挂断") + case 0x8015:// 找手机 + print("找手机") + case 0x8016:// 历史心率数据上报 + print("历史心率数据上报") + case 0x8017:// 历史血氧数据上报 + print("历史血氧数据上报") + case 0x8018:// 历史体温数据上报 + print("历史体温数据上报") + case 0x8019:// 睡眠数据上报 + print("睡眠数据上报") + case 0x8020:// 历史压力数据上报 + print("历史压力数据上报") + default: + break + } +// switch cmd { +// case .set, .find, .device, .remind, .firmware: +// parseSetData(SetCmd(rawValue: bytes[10]), data: data) +// case .sync: +// let valueBytes = Array(bytes[13.. 12 else { return } - let msgId = Int(UInt16(bytes[4]) << 8 | UInt16(bytes[5])) - let data = Data(bytes: bytes, count: bytes.count) - switch msgId { - case 0x8001: - newParseDefaultResponseData(bytes) - default: - break - } -// switch cmd { -// case .set, .find, .device, .remind, .firmware: -// parseSetData(SetCmd(rawValue: bytes[10]), data: data) -// case .sync: -// let valueBytes = Array(bytes[13..