Skip to content

Commit

Permalink
Merge pull request #264 from NordicSemiconductor/code-cleanup
Browse files Browse the repository at this point in the history
Minor ScannerViewController Code Cleanup
  • Loading branch information
dinesharjani authored Oct 9, 2024
2 parents c8ab852 + cbd5e90 commit 306560c
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,34 @@ class ScannerViewController: UITableViewController, CBCentralManagerDelegate, UI
}

// MARK: - CBCentralManagerDelegate

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// Find peripheral among already discovered ones, or create a new
// object if it is a new one.
var discoveredPeripheral = discoveredPeripherals.first(where: { $0.basePeripheral.identifier == peripheral.identifier })
var discoveredPeripheral: DiscoveredPeripheral! = discoveredPeripherals.first(where: {
$0.basePeripheral.identifier == peripheral.identifier
})
if discoveredPeripheral == nil {
discoveredPeripheral = DiscoveredPeripheral(peripheral)
discoveredPeripherals.append(discoveredPeripheral!)
discoveredPeripherals.append(discoveredPeripheral)
}

// Update the object with new values.
discoveredPeripheral!.update(withAdvertisementData: advertisementData, andRSSI: RSSI)
discoveredPeripheral.update(withAdvertisementData: advertisementData, andRSSI: RSSI)

// If the device is already on the filtered list, update it.
// It will be shown even if the advertising packet is no longer
// matching the filter. We don't want any blinking on the device list.
if let index = filteredPeripherals.firstIndex(of: discoveredPeripheral!) {
if let index = filteredPeripherals.firstIndex(of: discoveredPeripheral) {
// Update the cell views directly, without refreshing the
// whole table.
if let aCell = tableView.cellForRow(at: [0, index]) as? ScannerTableViewCell {
aCell.peripheralUpdatedAdvertisementData(discoveredPeripheral!)
if let cell = tableView.cellForRow(at: [0, index]) as? ScannerTableViewCell {
cell.peripheralUpdatedAdvertisementData(discoveredPeripheral)
}
} else {
// Check if the peripheral matches the current filters.
if matchesFilters(discoveredPeripheral!) {
filteredPeripherals.append(discoveredPeripheral!)
if matchesFilters(discoveredPeripheral) {
filteredPeripherals.append(discoveredPeripheral)
tableView.reloadData()
}
}
Expand Down

0 comments on commit 306560c

Please sign in to comment.