我正在与le蓝牙项目合作,并且有3个View Controller:MainMenuViewController,ViewController1和ViewController2。当我从MainMenuViewController转到ViewController1或ViewController2时,我可以连接并控制我的蓝牙设备。但是,如果我返回MainMenuViewController,然后尝试使用ViewController1或ViewController2,则什么都不会发生。我可以连接到设备(至少可以用选择器来连接它),但不能控制它。 我使用导航控制器和Show Segues进行导航。如何在控制器之间导航并能够重新连接到设备并对其进行控制?
这是一段VC1代码。 VC2看起来很相似
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.isIdleTimerDisabled = true
NotificationCenter.default.addObserver(self, selector: #selector(accessoryConnected), name: NSNotification.Name.EAAccessoryDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accessoryDisconnected), name: NSNotification.Name.EAAccessoryDidDisconnect, object: nil)
EAAccessoryManager.shared().registerForLocalNotifications()
EAAccessoryManager.shared().showBluetoothAccessoryPicker(withNameFilter: nil, completion: {(error: NSError?) in
} as? EABluetoothAccessoryPickerCompletion)
////在这里,当我按Back时试图关闭连接,但是回到该VC后,这并不能帮助我控制设备
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
debugPrint("Back Button pressed.")
stopVC1()
}
}
func stopVC1 () {
self.stopTimer()
self.brick?.closeConnection()
close(0)
}