didDisconnectPeripheral function is not being called

假装没事ソ 提交于 2020-01-07 02:29:40

问题


I'm going to break down what the flow for connection is for the first time a user wants to connect to a BLE Peripheral Device. I think I may be doing something that upsets the natural flow of the CoreBluetooth Module. My app currently works with regards to connecting and sending commands to a BLE device.

To connect, I first land on a page with a tableview which displays the devices available to connect to.

override func viewDidLoad() {
        super.viewDidLoad()

        //self.centralManager = CBCentralManager(delegate: blueCoreDelegate, queue: nil)
        self.centralManager = CBCentralManager(delegate: self, queue: nil)

        // setting the central manager in BlueCoreManager as the same
        BlueCoreManager.shared.getCentralManager(centralManager!)

    }

BlueCoreManager is a singleton that I have created to easily manage multiple connections. At this point the centralManagerDidUpdateState function is called. I know that it goes to the centralManagerDidUpdateState function in both the current page and the singleton. In both I scan for peripherals.

In the current page: I append the discovered peripherals onto the tableview.

In the singleton: I keep track the connection states

Now once a cell is tapped on the current page, I connect to it:

// Connect to peripheral on tap
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let peripheral = peripherals[indexPath.row]

        if peripheral.name != nil {

            self.centralManager!.connectPeripheral(peripheral, options: nil)
            print("Connecting to \(peripheral.name!)")

        }

    }

After the above, the didConnectPeripheral on the currentPage is called, where I update connectionStates and call the syncPeripheral function in the singleton.

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {

        // setting connection state to 4 (currently connected)
        let cstate = over.valueForKey("ConnectionState") as! Int

        if cstate == 4 {

            print("ConnectionState: One Connected. Connecting to Second")
            over.setObject(5, forKey: "ConnectionState")
            over.synchronize()
            print("Connected To Two")
        } 
.......

.......

         else if cstate == 7 {

            print("ConnectionState: Disconnected from secondary. Connecting to Second")
            over.setObject(5, forKey: "ConnectionState")
            over.synchronize()
            print("Connected To Two")

        }

        // synchronizing the current peripheral in BlueCoreManager
        BlueCoreManager.shared.syncPeripheral(peripheral)

        displayAlert("Connected!", message: "You are connected to \(peripheral.name!)")


    }

This is the syncPeripheral function which is then called, where I set the peripherals' delegates and call discoverServices whichever one is connected.

func syncPeripheral(peripheral: CBPeripheral) {
        let cstate = modeData.valueForKey("ConnectionState")

        switch(cstate as! Int) {
        case 0: print("Connection State Powered On")
                print("Not keeping any reference")

        case 4: print("Connection State To One")
                self.easeDevice = peripheral
                print(peripheral.name!)
                self.easeDevice!.delegate = self
                self.connectedPeripheralArray.insert(peripheral, atIndex: 0)
                self.familiarPeripherals.insert(peripheral, atIndex: 0)
                print(connectedPeripheralArray[0].name)
            self.easeDevice!.discoverServices(nil)

        case 5: print("Connection State To Two")
                self.phy = peripheral
                print(peripheral.name!)
                self.phy!.delegate = self
                self.connectedPeripheralArray.insert(peripheral, atIndex: 1)
                self.familiarPeripherals.insert(peripheral, atIndex: 1)
            self.phy!.discoverServices(nil)
        case 6: print("Connection State Disconnected from One")
        case 7: print("Connection State Disconnected from device two")
        default: print("Connection State = \(cstate)")
        }

    }

I then discover the services and characteristics in the singleton NSObject class. In the disconnect function, I'm updating the disconnect states and trying to reconnect to the disconnected peripheral. Also I'm passing the disconnected peripheral through a protocol I defined.

// If disconnected, connect again
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {

// Delegate which passes disconnected peripherals
if delegate != nil {

    delegate!.peripheralDidDisconnect("\(peripheral.name!)")

}

print("Disconnected")
if peripheral == self.easeDevice {

    modeData.setObject(6, forKey: "ConnectionState")
    modeData.synchronize()
    connectedPeripheralArray.removeAtIndex(0)
    self.centralManager!.connectPeripheral(peripheral, options: nil)

} else if peripheral == self.phy {

    modeData.setObject(7, forKey: "ConnectionState")
    modeData.synchronize()
    connectedPeripheralArray.removeAtIndex(1)
    self.centralManager!.connectPeripheral(peripheral, options: nil)

}

}

The only problem is that the above function never gets called on the singleton object (The singleton is an NSObject class, could this be the reason why?). What am I missing?! This issue is driving me insane! Any insights are highly appreciated.

来源:https://stackoverflow.com/questions/37789690/diddisconnectperipheral-function-is-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!