title: Disconnect a BLE peripheral in SWIFT
body: <p>I have some issues to disconnect a BLE peripheral in Swift. First, I tried to use only the <code>cancelPeripheralConnection:</code> function. But if I just call this function, the <code>didDisconnectPeripheral</code> function is never called. So I tried to follow <a href="https://developer.apple.com/library/prerelease/mac/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html" rel="noreferrer">Apple's</a> reference guide. There is say's, that you should delete every notifications before disconnecting. Is this really necessary? And is there a possibility to cancel all notifications in one step? I set up a lot of notifications, so I have to search in many services and characteristics to reset them. I guess, that can't be a "well done" solution.</p>
  
  <p><strong>EDIT:</strong>
  Okay I figured out, that the <code>cancelPeripheralConnection</code> works pretty well, if I call it in my <code>BluetoothManager</code> class, where <code>CBCentralManager</code> and <code>CBPeripheralDelegate</code> are included... Is there a way to disconnect to a peripheral outside of this function?</p>
  
  <p><strong>EDIT 4:</strong></p>
  
  <pre><code>import UIKit
  
  class ValueCollectionView: UICollectionViewController
  {
      var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
      var bluetoothManager: BluetoothManager = BluetoothManager()
  
      override func viewDidLoad()
      {
          super.viewDidLoad()
          self.navigationItem.hidesBackButton = true
          let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
          self.navigationItem.leftBarButtonItem = newBackButton;
      }
  
      override func didReceiveMemoryWarning()
      {
          super.didReceiveMemoryWarning()
      }
  
      func back(sender: UIBarButtonItem)
      {
          bluetoothManager.disconnectPeripheral(selectedPeripheralIndex!)
          self.navigationController?.popViewControllerAnimated(true)
      }
  //Some Collection View functions...
  }
  </code></pre>
  
  <p>And this is my implementation of the <code>disconnectPeripheral</code> function (integrated in the BluetoothManager class):</p>
  
  <pre><code>func disconnectPeripheral(peripheralIndex: Int)
  {
      CBmanager.cancelPeripheralConnection(peripheralArray![peripheralIndex].peripheral)
  }
  </code></pre>
  
  <p>But anyway, if I call this function, the <code>didDisconnectPeripheral</code> function isn't called. When I put the function in the BluetoothManager class e.g. after I discovered the last characteristic, everything works. </p>
  
  <p><strong>EDIT 5:</strong></p>
  
  <pre><code>class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
  {
      var CBmanager: CBCentralManager = CBCentralManager()
  
      override init()
      {
          super.init()
          self.CBmanager = CBCentralManager(delegate: self, queue: nil)
      }
  
      func connectPeripheral(peripheralIndex: Int)
      {
          CBmanager.connectPeripheral(peripheralArray![peripheralIndex].peripheral, options: nil)
      }
  
      func disconnectPeripheral(peripheralIndex: Int)
      {
          CBmanager.cancelPeripheralConnection(peripheralArray![peripheralIndex].peripheral)
      }
  
  //The other CentralManager functions...
  
  }
  </code></pre>