-1

I'm working on a project with corebluetooth framework.When my central manager starts scanning for peripherals it connects to a single peripheral after sometime maybe one minute it starts connecting to multiple peripherals with same UUID.

Is there a way to tell the device to stick to one peripheral which is connected initially? Variables

public var centralManager: CBCentralManager? = nil
public let baseLayerServices = [SkiinUUID(string: SkiinPeripheral.baseLayerService)]
@Published public var peripherals: AllPeripherals = AllPeripherals()

Did update state

 public func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("state: \(self.getStateString())")
        if central.state == .poweredOn {
            self.showStateAlert = false

            if let connectedPeripherals =  self.centralManager?.retrieveConnectedPeripherals(withServices: self.baseLayerServices), connectedPeripherals.count > 0 {
                print("Already connected: \(connectedPeripherals.map{$0.name}), self.peripherals: \(self.peripherals)")
                self.centralManager?.stopScan()

            }
            else {
                print("scanForPeripherals")
                self.centralManager?.scanForPeripherals(withServices: self.baseLayerServices, options: nil)
            }
        }
        else {
            self.showStateAlert = true
        }
    }

didDiscover Peripheral Code

public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

        guard peripheral.state != .connected else {
            return
        }

        print("didDiscover: \(peripheral.name ?? peripheral.identifier.uuidString)")

        self.peripherals.baseLayer.top.add(cbPeripheral: peripheral)
        self.centralManager?.connect(peripheral, options: nil)
        self.observe(peripheral: self.peripherals.baseLayer.top)
    }
Denzil
  • 415
  • 5
  • 13
  • 1
    It is an important distinction to make. Is your device actually connecting to other devices? Or is it merely discovering them ? core bluetooth will constantly scan for available devices in its range. This discovery does not mean connection to the device. Please share your log output or any related code that may be useful to the community. – Julian Silvestri Nov 15 '19 at 20:40
  • Hey Julian it's connecting to other devices.This is post scanning and connecting to a device. – Denzil Nov 15 '19 at 21:29
  • Hey @JulianSilvestri I have added the code block – Denzil Nov 15 '19 at 21:38

1 Answers1

0

When this code discovers a peripheral, it tries to connect to it. I don't see anywhere that it stops scanning, and it doesn't avoid connecting just because it's connected.

If you don't want to keep scanning after you discover something, call stopScanning(). If you don't want to connect when you're already connected (or connecting), then don't call connect() in those cases.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610