我打算在用户第一次想要连接到BLE外围设备时分解连接流程。我想我可能会做一些扰乱CoreBluetooth模块自然流动的事情。我的应用程序目前适用于连接和发送命令到BLE设备。
要连接,我首先登陆一个带有tableview的页面,该桌面显示可连接的设备。
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是我创建的单例,可以轻松管理多个连接。此时调用centralManagerDidUpdateState函数。 我知道它会转到当前页面和单例中的centralManagerDidUpdateState函数。在这两个中我扫描外围设备。
在当前页面中:我将发现的外围设备附加到tableview上。
在单身人士中:我一直跟踪连接状态
现在,在当前页面上点击一个单元格后,我就连接到它:
// 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!)")
}
}
在上面之后,调用currentPage上的didConnectPeripheral,在那里我更新connectionStates并在单例中调用syncPeripheral函数。
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!)")
}
这是syncPeripheral函数,然后调用它,我设置外围设备的委托并调用discoverServices,无论哪个连接。
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)")
}
}
然后我发现了单例NSObject类中的服务和特征。在断开连接功能中,我正在更新断开连接状态并尝试重新连接到断开连接的外围设备。此外,我通过我定义的协议传递断开连接的外围设备。
// 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)
}
}
唯一的问题是上面的函数永远不会在单例对象上被调用(单例是一个NSObject类,这可能是为什么?)。我错过了什么?!这个问题让我疯了!任何见解都受到高度赞赏。