From 671a3d9e4f68a8db84cd9dabbd3f60d7bc86f544 Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 23 Jan 2024 22:28:26 +0800 Subject: [PATCH] refactor:replace the mtk with original code, for the mtk filter the peripheral's services, can't get all sevices and no way to do the ota update attension:redo the reconnect, triggered in init method instead --- HDFwear/Home/HomeViewController.swift | 2 +- HDFwear/Setting/SettingViewController.swift | 5 ++++- HDFwear/Tools/BluetoothManager+OTA.swift | 8 ++++++++ HDFwear/Tools/BluetoothManager.swift | 563 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4 files changed, 412 insertions(+), 166 deletions(-) create mode 100644 HDFwear/Tools/BluetoothManager+OTA.swift diff --git a/HDFwear/Home/HomeViewController.swift b/HDFwear/Home/HomeViewController.swift index e6bcb8d..f2c6fbf 100644 --- a/HDFwear/Home/HomeViewController.swift +++ b/HDFwear/Home/HomeViewController.swift @@ -93,7 +93,7 @@ class HomeViewController: UIViewController, CLLocationManagerDelegate { BluetoothManager.shared.startScanning()//modify断蓝牙连接不上Bug BluetoothManager.shared.registerDelegate(self) BluetoothManager.shared.registerSyncDelegate(self) - BluetoothManager.shared.reConnect() +// BluetoothManager.shared.reConnect() // updateStepView() if #available(iOS 14.0, *) { diff --git a/HDFwear/Setting/SettingViewController.swift b/HDFwear/Setting/SettingViewController.swift index 7a63d1b..3aa9e3a 100644 --- a/HDFwear/Setting/SettingViewController.swift +++ b/HDFwear/Setting/SettingViewController.swift @@ -82,6 +82,7 @@ class SettingViewController: UIViewController, LBXScanViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() + settingArray = setting_828 setupUI() BluetoothManager.shared.registerDelegate(self) @@ -554,7 +555,9 @@ extension SettingViewController: BluetoothManagerDelegate { GCDTimer.shared.cancleTimer(WithTimerName: "DevicePairing") } - collectionView.reloadData() + if settingArray.count > 1 { + collectionView.reloadData() + } scanView?.updateUI(.PairSuccess) // addDeviceView.isHidden = true // connectView.isHidden = false diff --git a/HDFwear/Tools/BluetoothManager+OTA.swift b/HDFwear/Tools/BluetoothManager+OTA.swift new file mode 100644 index 0000000..c4460a4 --- /dev/null +++ b/HDFwear/Tools/BluetoothManager+OTA.swift @@ -0,0 +1,8 @@ +// +// BluetoothManager+OTA.swift +// HDFwear +// +// Created by admin on 2024/1/23. +// + +import Foundation diff --git a/HDFwear/Tools/BluetoothManager.swift b/HDFwear/Tools/BluetoothManager.swift index ddc7f2c..caec5bc 100644 --- a/HDFwear/Tools/BluetoothManager.swift +++ b/HDFwear/Tools/BluetoothManager.swift @@ -69,6 +69,7 @@ extension BluetoothSyncDelegate { class BluetoothManager: NSObject { + private var peripheralUUIDKey = "lastConnectedPeripheralUUID" //Closure var setCmdClosure: ((_ error: Int?) -> ())? @@ -132,7 +133,7 @@ class BluetoothManager: NSObject { // weak var delegate: BluetoothManagerDelegate? - var manger: MTKBleManager? +// var manger: MTKBleManager? var peripheral: CBPeripheral? = nil // private var conPeripheral: CBPeripheral? = nil //已连接的 var platform: Platform = .other @@ -146,7 +147,8 @@ class BluetoothManager: NSObject { private var contactArray: [ContactModel] = [] //ota - var transceiver = LETransceiver() + var isOTAUpdating: Bool = false + var otaTransceiver : LETransceiver? var otaManager = OTAManager() //mergeData @@ -170,32 +172,34 @@ class BluetoothManager: NSObject { // manger.registerClientProfile(BLEConfig.MTKBasServerUUID, clientProfileDelegate: self) // manger.registerClientProfile(BLEConfig.MTKDialServerUUID, clientProfileDelegate: self) // FmpGattClient.getInstance() - manger = MTKBleManager.sharedInstance() as? MTKBleManager +// manger = MTKBleManager.sharedInstance() as? MTKBleManager platform = CurDevice.platform - setupManger(nil) +// setupManger(nil) // registerClientProfile() +// let options = ["CBCentralManagerOptionShowPowerAlertKey":false] + centralManger = CBCentralManager(delegate: self, queue: .main) } - private func setupManger(_ peripheral: CBPeripheral?) { - if peripheral == nil { - if CurDevice.uuid == "" { - return - } else { - registerClientProfile() - } - } else { - registerClientProfile() - } - } - - private func registerClientProfile() { - switch platform { - case ._828: - manger?.registerClientProfile(BLEConfig.ServerUUID, clientProfileDelegate: self) - default: - break - } - } +// private func setupManger(_ peripheral: CBPeripheral?) { +// if peripheral == nil { +// if CurDevice.uuid == "" { +// return +// } else { +// registerClientProfile() +// } +// } else { +// registerClientProfile() +// } +// } +// +// private func registerClientProfile() { +// switch platform { +// case ._828: +// manger?.registerClientProfile(BLEConfig.ServerUUID, clientProfileDelegate: self) +// default: +// break +// } +// } func registerDelegate(_ delegate: BluetoothManagerDelegate) { delegateList.append(delegate) @@ -223,10 +227,18 @@ class BluetoothManager: NSObject { print("jason_bluetooth_action_0:开始扫描") peripheralDic = [:] deviceArray = [] - centralManger = CBCentralManager(delegate: self, queue: nil) - - manger?.startScanning() -//mClientProfileDic __NSDictionaryM * 1 key/value pair 0x0000000281a694c0 + if centralManger != nil { +// getInfo(state: centralManger!.state) + centralManger!.scanForPeripherals(withServices: nil, options: nil) + } else { + let options = ["CBCentralManagerOptionShowPowerAlertKey":false] + centralManger = CBCentralManager(delegate: self, queue: .main, options: options) + centralManger!.scanForPeripherals(withServices: nil, options: nil) + } +// centralManger = CBCentralManager(delegate: self, queue: nil) +// +// manger?.startScanning() +////mClientProfileDic __NSDictionaryM * 1 key/value pair 0x0000000281a694c0 } func stopScanning() { @@ -234,9 +246,9 @@ class BluetoothManager: NSObject { if centralManger!.isScanning { centralManger?.stopScan() } - centralManger = nil +// centralManger = nil } - manger?.stopScanning() +// manger?.stopScanning() } @@ -246,8 +258,11 @@ class BluetoothManager: NSObject { let name = peripheral.name // 这里是根据名字来判断其平台类型 platform = ._828 - registerClientProfile() - manger?.connectPeripheral(peripheral) + if centralManger != nil { + centralManger!.connect(peripheral, options: nil) + } +// registerClientProfile() +// manger?.connectPeripheral(peripheral) } func reconnectPer() { @@ -260,16 +275,17 @@ class BluetoothManager: NSObject { func reConnect() { - startScanning() +// startScanning() + reconnectToPeripheral() } func forgetPeripheral() { print("forgetPeripheral") - manger?.forgetPeripheral() +// manger?.forgetPeripheral() // let menstrual = .menstrual - manger = nil +// manger = nil // MTKBleManager.sharedInstance() = nil - manger = MTKBleManager.sharedInstance() as? MTKBleManager +// manger = MTKBleManager.sharedInstance() as? MTKBleManager // OTAPeripheral = nil peripheral = nil @@ -280,8 +296,12 @@ class BluetoothManager: NSObject { func disconnect() { for per in connectedPerArray { - manger?.disconnectPeripheral(per) +// manger?.disconnectPeripheral(per) + if centralManger != nil { + centralManger!.cancelPeripheralConnection(per) + } } + connectedPerArray = [] // manger.disconnectPeripheral() // manger.foundPeripherals = [] @@ -296,10 +316,10 @@ class BluetoothManager: NSObject { disconnect() // peripheral = nil // manger.peripheral = nil - manger?.foundPeripherals = nil - manger?.tempPeripheral = nil - manger?.connectedService = nil - manger?.connectPeripherals = nil +// manger?.foundPeripherals = nil +// manger?.tempPeripheral = nil +// manger?.connectedService = nil +// manger?.connectPeripherals = nil // if centralManger != nil { // if centralManger!.isScanning { // centralManger?.stopScan() @@ -1342,24 +1362,243 @@ class BluetoothManager: NSObject { } -extension BluetoothManager: ClientProfileDelegate { - func onCentralManagerStateChange(_ state: Int32) { -// var managerState: CBCentralManagerState = .unknown - switch state { - case 0: -// managerState = .unknown +//extension BluetoothManager: ClientProfileDelegate { +// func onCentralManagerStateChange(_ state: Int32) { +//// var managerState: CBCentralManagerState = .unknown +// switch state { +// case 0: +//// managerState = .unknown +// print("unknown") +// case 1: +//// managerState = .resetting +// print("resetting") +// case 2: +//// managerState = .unsupported +// print("unsupported") +// case 3: +//// managerState = .unauthorized +// print("unauthorized") +// case 4: +//// managerState = .poweredOff +// print("poweredOff") +// if DeviceIsConnected { +// DeviceIsConnected = false +// for delegate in delegateList { +// delegate.didDisconnected() +// } +// } +// case 5: +// print("poweredOn") +//// managerState = .poweredOn +// centralManger?.scanForPeripherals(withServices: nil, options: nil) +// default: +// print(" ") +// } +// +// for delegate in delegateList { +// delegate.centralManagerDidUpdateState(Int(state)) +// } +// } +// +// func onConnected(_ peripheral: CBPeripheral!) { +// print("jason_bluetooth_action_3:通过MTKBleManager的ClientProfileDelegate得到连接成功回调"); +// print("已经连接: \(peripheral.identifier.uuidString)--\(peripheral.name ?? "")") +// +// stopScanning() +// connectedPerArray.append(peripheral) +// self.peripheral = peripheral +// print("jason_bluetooth_action_4:开始发现外设服务"); +// peripheral.discoverServices(nil) +// +//// var delayTime: Double = 2 +// +// if CurDevice.uuid == "" { +// var mac = "" +// for device in deviceArray { +// if peripheral!.identifier.uuidString == device.peripheral.identifier.uuidString { +// mac = device.mac +// } +// } +// +// let admin = AdminHelper.shared.loadLocalAdminData() +// if admin.userInfo.mac != mac { +// print("不同设备") +// admin.userInfo.mac = mac +// admin.userInfo.dailVersion = "0" +// admin.userInfo.isDailSync = false +// admin.userInfo.dailIndex = -1 +// admin.userInfo.dialIndexPath = "" +// admin.userInfo.customDial = "" +//// admin.userInfo.dialNumDic = [:] +// } +// admin.device = DeviceModel(uuid: peripheral!.identifier.uuidString, name: peripheral!.name ?? "", platform: platform, mac: mac) +// +// AdminHelper.shared.savaAdminInfo(admin) +//// AdminHelper.shared.updateDevice(DeviceModel(uuid: peripheral!.identifier.uuidString, name: peripheral!.name ?? "", platform: platform, mac: mac)) +//// delayTime = 3 +//// GCDTimer.shared.scheduledDispatchTimerNotNow(WithTimerName: "SyncLanguage", timeInterval: 2, queue: .main, repeats: false) { +//// self.syncLanguage() +//// } +// } +// +// if platform == ._828 { +// print("828连接") +//// OTAProfile = RTKOTAProfile(delegate: self) +// //j!!! 取消自动同步 +//// startSync828(delayTime) +// } +// +// DeviceIsConnected = true +//// MusicManager.shared.controlMusic() +// for delegate in delegateList { +// delegate.didConnect(peripheral, CurDevice.mac) +// } +// +//// GCDTimer.shared.scheduledDispatchTimerNotNow(WithTimerName: "FirstSync", timeInterval: 5+delayTime, queue: .main, repeats: false) { +//// self.syncBug() +//// for delegate in self.syncDelegateList { +//// delegate.firstSync() +//// } +//// } +//// +//// isFirstSync = true +// } +// +// func onDisconnected() { +// for delegate in delegateList { +// delegate.didDisconnected() +// } +// print("断开连接1") +// } +// +// func onServiceDiscovered(_ periphearl: CBPeripheral!, error: Error!) { +// print("jason_bluetooth_action_5:通过MTKBleManager的ClientProfileDelegate得到发现服务成功回调,这里应该是SDK做了相关后续动作,直接触发后续回调"); +//// print("已经连接2") +// } +// +// func onCharacteristicDiscovered(_ peripheral: CBPeripheral!, for service: CBService!, error err: Error!) { +// print("jason_bluetooth_action_6:通过MTKBleManager的ClientProfileDelegate得到发现特征回调"); +// if service.characteristics == nil { +// return +// } +// for char in service.characteristics! { +// let uuid = char.uuid.uuidString +// // 这里读出来的uuid被截断,应该是本身数据有问题 +//// switch uuid { +//// case BLEConfig.MTKReadUUID, BLEConfig.MTKBasReadUUID, BLEConfig.MTKDialReadUUID, BLEConfig.ReadUUID: +//// peripheral.setNotifyValue(true, for: char) +//// default: +//// break +//// } +// if BLEConfig.ReadUUID .contains(uuid) { +// peripheral.setNotifyValue(true, for: char) +// } +// } +// } +// //MARK: - 接收数据 +// func onUpdateValue(forCharacteristic peripheral: CBPeripheral!, for characteristic: CBCharacteristic!, error err: Error!) { +// guard let data = characteristic.value, data.count >= 0 else { +// return +// } +// +// switch characteristic.uuid.uuidString { +// case BLEConfig.MTKBasReadUUID: +// print("电量------\([UInt8](data))") +// Battery = Int([UInt8](data)[0]) +// for delegate in syncDelegateList { +// delegate.didReceiveBattery(value: Battery) +// } +// case BLEConfig.MTKDialReadUUID: +// print("表盘------\([UInt8](data))") +// let bytes = [UInt8](data) +// if bytes.count > 13 { +// if bytes[13] == 1 { +// sendNextSyncData() +// } else if bytes[13] == 2 { +//// sendDialData(dialRemainData) +// } +// } +// case BLEConfig.ReadUUID: +// print("接收: \([UInt8](data))") +// mergeData(data) +// default: +// print("接收: \([UInt8](data))") +// mergeData(data) +// } +// } +// +// func mergeData(_ data: Data) { +// let bytes = [UInt8](data) +//// print(bytes) +// isSyncReceived = true +// if bytes[0] == 0xED && bytes[1] == 0x7E { +//// if bytes.count > 10, BleCmd(rawValue: bytes[8]) == .sync { +//// if isSync { +//// if SyncCmd(rawValue: bytes[10]) != isSyncType { +//// syncReceiveFail() +//// } else { +//// isNextSync = bytes[1] == 112 +//// startSyncTimer() +//// isSyncReceived = true +//// } +//// } +//// } +// // 内容长度加上其他位的长度 +// totalLength = Int(UInt16(bytes[8]) << 8 | UInt16(bytes[9])) + 12 +// if data.count == totalLength { +// parseData(bytes) +// } else { +// totalBytes = [] +// totalBytes.append(contentsOf: bytes) +// mergeLength = bytes.count +// } +// } else { +// if isSync { +// if isSyncReceived { +// startSyncTimer() +// isSyncReceived = true +// } +// } +// totalBytes.append(contentsOf: bytes) +// mergeLength += bytes.count +// if mergeLength == totalLength { +// parseData(totalBytes) +// } +// } +// +// +// } +// +// +// func onCharacteristicWrite() { +//// print("已经连接5") +// } +// +// func onReadRssi(_ peripheral: CBPeripheral!, rssiValue rssi: Int32, error err: Error!) { +//// print("已经连接6") +// } +//} + + + +extension BluetoothManager: CBCentralManagerDelegate { + func centralManagerDidUpdateState(_ central: CBCentralManager) { + // var managerState: CBCentralManagerState = .unknown + switch central.state { + case .unknown: + // managerState = .unknown print("unknown") - case 1: -// managerState = .resetting + case .resetting: + // managerState = .resetting print("resetting") - case 2: -// managerState = .unsupported + case .unsupported: + // managerState = .unsupported print("unsupported") - case 3: -// managerState = .unauthorized + case .unauthorized: + // managerState = .unauthorized print("unauthorized") - case 4: -// managerState = .poweredOff + case .poweredOff: + // managerState = .poweredOff print("poweredOff") if DeviceIsConnected { DeviceIsConnected = false @@ -1367,27 +1606,93 @@ extension BluetoothManager: ClientProfileDelegate { delegate.didDisconnected() } } - case 5: + case .poweredOn: print("poweredOn") -// managerState = .poweredOn - centralManger?.scanForPeripherals(withServices: nil, options: nil) + // managerState = .poweredOn + reconnectToPeripheral() +// centralManger?.scanForPeripherals(withServices: nil, options: nil) default: print(" ") } for delegate in delegateList { - delegate.centralManagerDidUpdateState(Int(state)) + delegate.centralManagerDidUpdateState(Int(central.state.rawValue)) } } - func onConnected(_ peripheral: CBPeripheral!) { - print("jason_bluetooth_action_3:通过MTKBleManager的ClientProfileDelegate得到连接成功回调"); - print("已经连接: \(peripheral.identifier.uuidString)--\(peripheral.name ?? "")") + func reconnectToPeripheral() { + print("try reconnectToPeripheral") + guard let savedUUIDString = UserDefaults.standard.string(forKey: peripheralUUIDKey), + let savedUUID = UUID(uuidString: savedUUIDString) else { + // 没有保存的设备UUID,可能是首次连接 + return + } + + let peripherals = centralManger?.retrievePeripherals(withIdentifiers: [savedUUID]) + if let peripheral = peripherals?.first { + self.peripheral = peripheral + self.peripheral?.delegate = self + print("reconnectToPeripheral work") + centralManger?.connect(peripheral, options: nil) + } + } + func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { + print("jason_bluetooth_action_1:通过CBCentralManagerDelegate回调得到扫描结果"); + print("jason " + (peripheral.name ?? "")); + if peripheralDic.keys.contains(peripheral.identifier.uuidString) { + return + } +// if peripheral.name?.range(of: "You 1_LE") != nil || peripheral.name?.range(of: "Pro_LE") != nil || peripheral.name?.range(of: "MTB025B") != nil || peripheral.name?.range(of: "MTB033B") != nil || peripheral.name?.range(of: "hq7") != nil || peripheral.name?.range(of: "D09") != nil || peripheral.name?.range(of: "P8GT") != nil || peripheral.name?.range(of: "hq8") != nil || peripheral.name?.range(of: "Watch 8") != nil || peripheral.name?.range(of: "D07") != nil || peripheral.name?.range(of: "S7 NO.1") != nil || peripheral.name?.range(of: "G7 Pro Max Gameboy") != nil { +// if peripheral.name?.range(of: "KV-S10f44efd8deb8b") != nil { +// if peripheral.name?.range(of: "CHiQ Compass_50c0f0668129") != nil { + if peripheral.name?.range(of: "CHiQ") != nil { + peripheralDic[peripheral.identifier.uuidString] = peripheral + // print(peripheral.name)key String "kCBAdvDataServiceData" + + + if let data = advertisementData["kCBAdvDataManufacturerData"] as? Data { + var mac = data.hexToString(":") + mac = mac.substring(fromIndex: mac.length-17) + deviceArray.append(ScanDevice(peripheral, mac: mac)) + for delegate in delegateList { + delegate.didDiscover(devices: deviceArray) + } + } else { + if let json = advertisementData["kCBAdvDataServiceData"] { + if let dic = json as? Dictionary { + if let data = dic.values.first { + var mac = data.hexToString(":") + mac = mac.substring(fromIndex: mac.length-17) + deviceArray.append(ScanDevice(peripheral, mac: mac)) + for delegate in delegateList { + delegate.didDiscover(devices: deviceArray) + } + } + } + }else {// 当无值时直接给个值跑通调试 + var mac = "111" + deviceArray.append(ScanDevice(peripheral, mac: mac)) + for delegate in delegateList { + delegate.didDiscover(devices: deviceArray) + } + } + } + } + } + + func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { + print("jason_bluetooth_action_3:得到连接成功回调"); + print("已经连接: \(peripheral.identifier.uuidString)--\(peripheral.name ?? "")") + // 连接成功,保存设备 UUID + UserDefaults.standard.set(peripheral.identifier.uuidString, forKey: peripheralUUIDKey) + UserDefaults.standard.synchronize() + stopScanning() connectedPerArray.append(peripheral) self.peripheral = peripheral print("jason_bluetooth_action_4:开始发现外设服务"); + peripheral.delegate = self; peripheral.discoverServices(nil) // var delayTime: Double = 2 @@ -1395,7 +1700,7 @@ extension BluetoothManager: ClientProfileDelegate { if CurDevice.uuid == "" { var mac = "" for device in deviceArray { - if peripheral!.identifier.uuidString == device.peripheral.identifier.uuidString { + if peripheral.identifier.uuidString == device.peripheral.identifier.uuidString { mac = device.mac } } @@ -1411,7 +1716,7 @@ extension BluetoothManager: ClientProfileDelegate { admin.userInfo.customDial = "" // admin.userInfo.dialNumDic = [:] } - admin.device = DeviceModel(uuid: peripheral!.identifier.uuidString, name: peripheral!.name ?? "", platform: platform, mac: mac) + admin.device = DeviceModel(uuid: peripheral.identifier.uuidString, name: peripheral.name ?? "", platform: platform, mac: mac) AdminHelper.shared.savaAdminInfo(admin) // AdminHelper.shared.updateDevice(DeviceModel(uuid: peripheral!.identifier.uuidString, name: peripheral!.name ?? "", platform: platform, mac: mac)) @@ -1444,20 +1749,34 @@ extension BluetoothManager: ClientProfileDelegate { // isFirstSync = true } - func onDisconnected() { - for delegate in delegateList { - delegate.didDisconnected() - } - print("断开连接1") + func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { + } - func onServiceDiscovered(_ periphearl: CBPeripheral!, error: Error!) { - print("jason_bluetooth_action_5:通过MTKBleManager的ClientProfileDelegate得到发现服务成功回调,这里应该是SDK做了相关后续动作,直接触发后续回调"); -// print("已经连接2") + func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { + + } +} + +extension BluetoothManager: CBPeripheralDelegate { + func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { + print("didDiscoverServices: \(peripheral.name ?? "")") + + guard error == nil else { + print("Error discovering service: \(error!.localizedDescription)") + return + } + + if let services = peripheral.services { + for service in services { + print("service: \(service)") + peripheral.discoverCharacteristics(nil, for: service) + } + } } - func onCharacteristicDiscovered(_ peripheral: CBPeripheral!, for service: CBService!, error err: Error!) { - print("jason_bluetooth_action_6:通过MTKBleManager的ClientProfileDelegate得到发现特征回调"); + func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { + print("jason_bluetooth_action_6:得到发现特征回调"); if service.characteristics == nil { return } @@ -1475,8 +1794,12 @@ extension BluetoothManager: ClientProfileDelegate { } } } - //MARK: - 接收数据 - func onUpdateValue(forCharacteristic peripheral: CBPeripheral!, for characteristic: CBCharacteristic!, error err: Error!) { + + func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { + + } + + func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { guard let data = characteristic.value, data.count >= 0 else { return } @@ -1545,95 +1868,7 @@ extension BluetoothManager: ClientProfileDelegate { parseData(totalBytes) } } - - } - - - func onCharacteristicWrite() { -// print("已经连接5") - } - - func onReadRssi(_ peripheral: CBPeripheral!, rssiValue rssi: Int32, error err: Error!) { -// print("已经连接6") - } -} - - - -extension BluetoothManager: CBCentralManagerDelegate { - func centralManagerDidUpdateState(_ central: CBCentralManager) { - switch central.state { - case .unknown: - print("unknown1") - case .resetting: - print("resetting1") - case .unsupported: - print("unsupported1") - case .unauthorized: - if let curVC = UIViewController.getCurrentViewController() { - curVC.showAlert(title: LocString("提示"), message: LocString("请在“设置-隐私-蓝牙”选项中,允许'YTWatch'使用蓝牙"), cancelText: nil, confirm: nil) - } - print("unauthorized1") - case .poweredOff: - print("poweredOff1") - case .poweredOn: - print("poweredOn1") - centralManger?.scanForPeripherals(withServices: nil, options: nil) - @unknown default: - print(" ") - } - - for delegate in delegateList { - delegate.centralManagerDidUpdateState(central.state.rawValue) - } - - } - - func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { - print("jason_bluetooth_action_1:通过CBCentralManagerDelegate回调得到扫描结果"); - print("jason " + (peripheral.name ?? "")); - if peripheralDic.keys.contains(peripheral.identifier.uuidString) { - return - } -// if peripheral.name?.range(of: "You 1_LE") != nil || peripheral.name?.range(of: "Pro_LE") != nil || peripheral.name?.range(of: "MTB025B") != nil || peripheral.name?.range(of: "MTB033B") != nil || peripheral.name?.range(of: "hq7") != nil || peripheral.name?.range(of: "D09") != nil || peripheral.name?.range(of: "P8GT") != nil || peripheral.name?.range(of: "hq8") != nil || peripheral.name?.range(of: "Watch 8") != nil || peripheral.name?.range(of: "D07") != nil || peripheral.name?.range(of: "S7 NO.1") != nil || peripheral.name?.range(of: "G7 Pro Max Gameboy") != nil { -// if peripheral.name?.range(of: "KV-S10f44efd8deb8b") != nil { -// if peripheral.name?.range(of: "CHiQ Compass_50c0f0668129") != nil { - if peripheral.name?.range(of: "CHiQ") != nil { - peripheralDic[peripheral.identifier.uuidString] = peripheral - // print(peripheral.name)key String "kCBAdvDataServiceData" - - - if let data = advertisementData["kCBAdvDataManufacturerData"] as? Data { - var mac = data.hexToString(":") - mac = mac.substring(fromIndex: mac.length-17) - deviceArray.append(ScanDevice(peripheral, mac: mac)) - for delegate in delegateList { - delegate.didDiscover(devices: deviceArray) - } - } else { - if let json = advertisementData["kCBAdvDataServiceData"] { - if let dic = json as? Dictionary { - if let data = dic.values.first { - var mac = data.hexToString(":") - mac = mac.substring(fromIndex: mac.length-17) - deviceArray.append(ScanDevice(peripheral, mac: mac)) - for delegate in delegateList { - delegate.didDiscover(devices: deviceArray) - } - } - } - }else {// 当无值时直接给个值跑通调试 - var mac = "111" - deviceArray.append(ScanDevice(peripheral, mac: mac)) - for delegate in delegateList { - delegate.didDiscover(devices: deviceArray) - } - } - } - } - } - } -- libgit2 0.21.4