0

I am a beginner to developing apps using Core Bluetooth. In my app, I am trying to show a device list (using a table view) of all discovered peripherals that are in range of the central. However, my device list keeps showing some peripherals in multiple tableview cells (duplicates), and I am unable to prevent this from happening despite having tried several counter methods. Moreover, none of the methods I try give me explicit error messages.

I have tried the following:

  1. Filtering new peripherals using their MAC adresses (where only ones with an address not possessed by other peripherals in the list are appended to the list).
  2. Setting the option "CBCentralManagerScanOptionAllowDuplicatesKey" to false when scanning for peripherals.
  3. Since actions are done to each unique peripheral discovered in the didDiscoverPeripherals central manager delegate function, I tried nesting this function in another function, with the new function looping through the didDiscoverPeripherals function for each newly discovered peripheral and then filtering duplicates outside the loop.
  4. In last ditch efforts I tried using the Array(set(MyArray)) and removeDuplicates() functions in this link.

Some of the other questions I referred to are:

  1. how to prevent duplicate peripherals in corebletooth?

  2. OSX Bluetooth scanning detects duplicate peripherals

To give a better background of the current status of my code, shown below is the snippet where I am encountering my problem:

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
                            advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print("Your device has discovered peripheral repeaters!")
        
        if repeater_peripheral.count < 5 {
            print("Waiting to discover more peripherals...")
            if repeater_peripheral.count > 0 {
                for existing in repeater_peripheral {
                    if existing.identifier != peripheral.identifier {
                        repeater_peripheral.append(peripheral)
                    } else {
                        return
                    }
                }
            } else {
                repeater_peripheral.append(peripheral)
            }
            self.centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])
        } else {
            centralManager.stopScan()
        }
        
        for peripheral in repeater_peripheral {
            peripheral.delegate = self
                        
            if peripheral.name != nil {
                repeater_items.append("\(String(describing: peripheral.name!))")
                print("You have successfully found and added a peripheral's name to your existing array!")
                repeater_subitems.append("\(peripheral.identifier)")
                print("You have successfully found and added a peripheral's identifier to your existing list!")
                } else {
                repeater_items.append("No Name")
                print("You have successfully found a peripheral but it has no name!")
                repeater_subitems.append("\(peripheral.identifier))")
                print("You have successfully found and added a peripheral's identifier to your existing list!")
            }
        }
        tableView.reloadData()
    }
}

I would greatly appreciate support on this to help me learn more on this subject! Thanks.

0 Answers0