3

I'm trying to disconnect from a BLE device. I'm doing cancelPeripheralConnection() and its never calling didDisconnectPeripheral(). I'm setting notify value to one characteristic, so while disconnecting i'm setting it false.

{

BLEOperations.sharedInstance.connectedDevice?.setNotifyValue(false, for: BLEOperations.sharedInstance.peripheralCharacteristics[0])

BLEOperations.sharedInstance.manager.cancelPeripheralConnection(BLEOperations.sharedInstance.connectedDevice!)

}

I'm trying to disconnect connectedPeripheral when user tries to connect to new device. The current device is not disconnected but new device is connected along with old one.

{

BLEOperations.sharedInstance.manager.connect(self.peripheralArray[indexPath.row] as! CBPeripheral , options: nil)

}

What am i doing wrong here? When device connection is lost the method is being called.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Hemanth
  • 133
  • 2
  • 12

3 Answers3

2

It will work fine when the CentralManager has only one instance in the entire flow.

Instead of using centralmanagerdelegates in viewcontroller, you can create a CentralManager operations class (which is a singleton) and perform operations.

I did that and its working fine.

Hemanth
  • 133
  • 2
  • 12
1

Well, probably we would need more information about your code. But the method you are looking for, is this one.

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    disconnected(central: central)
}

You should set your centralManager's delegate to self, and everytime a peripheral is disconnected it should call the delegate. Doing some tests, the disconnection process is not as nice as it seems, you can see your peripheral is still disconnecting but the delegate has been called (this can be adjusted with the hardware itself or the firmware on your peripheral).

BTW, you can connect several devices at once.

Alex Tarragó
  • 956
  • 8
  • 16
1

Cancels an active or pending connection to peripheral

class ViewController: UIViewController, CBCentralManagerDelegate {

  var manager: CBCentralManager!
  var peripherals: [CBPeripheral]?

  @IBAction func actionScanBLEDevice(_ sender: AnyObject) {
     manager = CBCentralManager (delegate: self, queue: nil)
  }

  @IBAction func actionDisconnectBLE(_ sender: AnyObject) {
     //Use As per need where you want to disconnect perticular  peripheral
     manager.cancelPeripheralConnection(peripheral[0]) // Disconnent numbers of BLE Devices
  }

  func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
     //Manage Some Condition then disconnect
     print("Disconnected from peripheral")  
     peripherals?.append(peripheral)
  }  

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

    print("Discovered peripheral \(peripheral.name,peripheral.identifier.uuidString)")
    print("advertisementData\(advertisementData)")
    // Use discovered peripheral here 
  }

  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print("Checking")
    switch(central.state)
    {
    case.unsupported:
        print("BLE is not supported")
    case.unauthorized:
        print("BLE is unauthorized")
    case.unknown:
        print("BLE is Unknown")
    case.resetting:
        print("BLE is Resetting")
    case.poweredOff:
        print("BLE service is powered off")
    case.poweredOn:
        print("BLE service is powered on")
        print("Start Scanning")
        manager.scanForPeripherals(withServices: nil, options: nil)
    }
  }

}
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • 1
    I'm using all the above suggested answers but didDisconnectPeripheral is being called when i power off the BLE device, but not called when i use manager.cancelPeripheralConnection(connectedDevice) – Hemanth Jan 30 '17 at 10:42