14 C
London
Monday, September 9, 2024

ios – Must replace UITableViewCell knowledge actual time


Im engaged on bluetooth software getting machine knowledge from broadcasting gadgets, all knowledge coming with out any challenge, this code we use to check our gadgets and this code base filtering solely these gadgets

let deviceLocations: [String: String] = [
“00000534”: “Office room”,
“00000523”: “Reception”,
“00000525”: “Lunch room”
]

I added for that function
All working with out any any challenge

I’ve tableview its exhibiting Machine Identify, UUID, RSSI and the Location

My query is Im getting realtime values from the gadgets however that values not updating within the desk view cell.

have to replace desk view knowledge in actual time

want to say I added refresher for five seconds however nonetheless not working

I’ve tachhed my code bellow. anybody please assist me to repair this challenge

import UIKit
import CoreBluetooth
import Mix

class DeviceListViewController: UIViewController, BluetoothManagerDelegate, UITableViewDelegate, UITableViewDataSource, CBCentralManagerDelegate {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var strongestRSSILabel: UILabel!
    
    var bluetoothManager: BluetoothManager!
    var centralManager: CBCentralManager!
    var discoveredDevices: [CBPeripheral] = []
    var rssiValues: [CBPeripheral: NSNumber] = [:]
    var rssiValue: Int = 0
    var rssiUpdateTimer: Timer?
    var deviceInfoArray: [DeviceInfo] = []
    
    @Printed var strongestRSSI: NSNumber?
    var strongestPeripheral: CBPeripheral?
    
    let deviceLocations: [String: String] = [
        "00000534": "Office room",
        "00000523": "Reception",
        "00000525": "Lunch room"
    ]
    
    non-public var commentary: AnyCancellable?
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        centralManager = CBCentralManager(delegate: self, queue: nil)
        
        bluetoothManager = BluetoothManager()
        bluetoothManager.delegate = self
        bluetoothManager.startScanning()
        
        commentary = $strongestRSSI
            .sink { [weak self] newValue in
                self?.tableView.reloadData()
            }
        
        refreshTableView()
        
        rssiUpdateTimer = Timer.scheduledTimer(timeInterval: 1.0, goal: self, selector: #selector(refreshTableView), userInfo: nil, repeats: true)
        RunLoop.present.add(rssiUpdateTimer!, forMode: .widespread)
    }
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            // Begin scanning for peripheral gadgets with RSSI values
            central.scanForPeripherals(withServices: nil, choices: nil)
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        // Replace the RSSI worth every time a peripheral is found
        rssiValue = RSSI.intValue
        
        // Chances are you'll need to replace your knowledge supply right here as properly
    }
    
    @objc func refreshTableView() {
        DispatchQueue.fundamental.async {
            self.filterDiscoveredDevices()
            self.filterDiscoveredDevicesRSSI()
            
            // Verify if strongestRSSI has modified and the strongestPeripheral is offered
            if let strongestRSSI = self.strongestRSSI,
               let strongestPeripheral = self.strongestPeripheral,
               let indexPath = self.indexPathForPeripheral(strongestPeripheral) {
                let cell = self.tableView.cellForRow(at: indexPath) as? DeviceTableViewCell
                cell?.rssiLabel?.textual content = "RSSI: (strongestRSSI)"
            }
            
            self.tableView.reloadData()
            print("Desk view refreshed ======================================== HHHH")
        }
    }
    
    // Perform to search out the IndexPath for a given peripheral
    non-public func indexPathForPeripheral(_ peripheral: CBPeripheral) -> IndexPath? {
        for (index, machine) in discoveredDevices.enumerated() {
            if machine === peripheral {
                return IndexPath(row: index, part: 0)
            }
        }
        return nil
    }
    
    func filterDiscoveredDevices() {
        discoveredDevices = discoveredDevices.filter { peripheral in
            if let title = peripheral.title {
                return ["00000534", "00000523", "00000525"].comprises(title)
            }
            return false
        }
    }
    
    func filterDiscoveredDevicesRSSI() {
        // Filter the found gadgets based mostly on the names
        let filteredDevices = discoveredDevices.filter { peripheral in
            if let title = peripheral.title {
                return ["00000534", "00000523", "00000525"].comprises(title)
            }
            return false
        }
        
        // Fetch RSSI values for the filtered gadgets
        for peripheral in filteredDevices {
            peripheral.readRSSI()
        }
        
        // Decide the strongest RSSI worth among the many filtered gadgets
        var strongestRSSI: NSNumber?
        var strongestLocation: String?
        
        for peripheral in filteredDevices {
            if let rssi = rssiValues[peripheral] {
                if strongestRSSI == nil || rssi.intValue > strongestRSSI!.intValue {
                    strongestRSSI = rssi
                    strongestLocation = deviceLocations[peripheral.name ?? ""]
                }
            }
        }
        
        // Replace strongestRSSI utilizing @Printed
        self.strongestRSSI = strongestRSSI
        
        // Replace the UILabel with the mixed label
        var combinedLabelText = "Strongest RSSI: N/A"
        if let strongestRSSI = strongestRSSI, let strongestLocation = strongestLocation {
            combinedLabelText = "Strongest RSSI: (strongestRSSI), Location: (strongestLocation)"
        }
        
        strongestRSSILabel.textual content = combinedLabelText
    }
    
    // Implement BluetoothManagerDelegate strategies
    func bluetoothManager(_ supervisor: BluetoothManager, didDiscoverPeripheral peripheral: CBPeripheral, withRSSI RSSI: NSNumber) {
        if !discoveredDevices.comprises(peripheral) {
            rssiValues[peripheral] = RSSI
            discoveredDevices.append(peripheral)
            filterDiscoveredDevices()
            filterDiscoveredDevicesRSSI()
            tableView.reloadData()
            
            // Verify if the RSSI is stronger than the present strongest RSSI
            if strongestRSSI == nil || RSSI.intValue > strongestRSSI!.intValue {
                strongestRSSI = RSSI
                strongestPeripheral = peripheral
            }
        }
    }

    
    func refreshRSSIValues() {
        for peripheral in discoveredDevices {
            peripheral.readRSSI()
        }
    }
    
    func bluetoothManager(_ supervisor: BluetoothManager, didStartAdvertisingPeripheral success: Bool) {
        if success {
            // Begin promoting
        } else {
            // Deal with promoting failure
        }
    }
    
    // Implement UITableViewDelegate and UITableViewDataSource strategies
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return discoveredDevices.depend
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceCell", for: indexPath) as! DeviceTableViewCell
        
        let peripheral = discoveredDevices[indexPath.row]
        cell.nameLabel?.textual content = "Machine Identify: (peripheral.title ?? "Unnamed Machine")"
        cell.uuidLabel?.textual content = "UUID: (peripheral.identifier.uuidString)"
        
        // Get the RSSI worth from the dictionary
        if let rssi = rssiValues[peripheral] {
            cell.rssiLabel?.textual content = "RSSI: (rssi)"
        } else {
            cell.rssiLabel?.textual content = "RSSI: N/A"
        }
        
        if let location = deviceLocations[peripheral.name ?? "Device Name"] {
            cell.locationLabel?.textual content = "Location: (location)"
        } else {
            cell.locationLabel?.textual content = "Location: N/A"
        }
        
        return cell
    }
}

Latest news

Relying on You

Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here