didDisconnectPeripheral 未调用 iOS Swift 3
didDisconnectPeripheral not called iOS Swift 3
我正在尝试断开与 BLE 设备的连接。我在做 cancelPeripheralConnection()
,它从不调用 didDisconnectPeripheral()
。
我将通知值设置为一个特征,因此在断开连接时我将其设置为 false。
{
BLEOperations.sharedInstance.connectedDevice?.setNotifyValue(false, for: BLEOperations.sharedInstance.peripheralCharacteristics[0])
BLEOperations.sharedInstance.manager.cancelPeripheralConnection(BLEOperations.sharedInstance.connectedDevice!)
}
当用户尝试连接到新设备时,我正在尝试断开 connectedPeripheral。当前设备未断开连接,但新设备与旧设备一起连接。
{
BLEOperations.sharedInstance.manager.connect(self.peripheralArray[indexPath.row] as! CBPeripheral , options: nil)
}
我在这里做错了什么?当设备连接丢失时,正在调用该方法。
好吧,我们可能需要有关您的代码的更多信息。但是你要找的方法就是这个
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
disconnected(central: central)
}
您应该将 centralManager 的委托设置为自己,并且每次外围设备断开连接时它都应该调用委托。
做了一些测试,断开连接的过程并不像看起来那么好,你可以看到你的外围设备仍然断开连接但是委托已经被调用(这可以通过硬件本身或你的外围设备上的固件来调整)。
顺便说一句,您可以同时连接多个设备。
取消与外围设备的活动或挂起连接
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)
}
}
}
当CentralManager在整个流程中只有一个实例时,它会正常工作。
您可以创建 CentralManager 操作 class(这是一个单例)并执行操作,而不是在 viewcontroller 中使用 centralmanagerdelegates。
我做到了,效果很好。
我正在尝试断开与 BLE 设备的连接。我在做 cancelPeripheralConnection()
,它从不调用 didDisconnectPeripheral()
。
我将通知值设置为一个特征,因此在断开连接时我将其设置为 false。
{
BLEOperations.sharedInstance.connectedDevice?.setNotifyValue(false, for: BLEOperations.sharedInstance.peripheralCharacteristics[0])
BLEOperations.sharedInstance.manager.cancelPeripheralConnection(BLEOperations.sharedInstance.connectedDevice!)
}
当用户尝试连接到新设备时,我正在尝试断开 connectedPeripheral。当前设备未断开连接,但新设备与旧设备一起连接。
{
BLEOperations.sharedInstance.manager.connect(self.peripheralArray[indexPath.row] as! CBPeripheral , options: nil)
}
我在这里做错了什么?当设备连接丢失时,正在调用该方法。
好吧,我们可能需要有关您的代码的更多信息。但是你要找的方法就是这个
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
disconnected(central: central)
}
您应该将 centralManager 的委托设置为自己,并且每次外围设备断开连接时它都应该调用委托。 做了一些测试,断开连接的过程并不像看起来那么好,你可以看到你的外围设备仍然断开连接但是委托已经被调用(这可以通过硬件本身或你的外围设备上的固件来调整)。
顺便说一句,您可以同时连接多个设备。
取消与外围设备的活动或挂起连接
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)
}
}
}
当CentralManager在整个流程中只有一个实例时,它会正常工作。
您可以创建 CentralManager 操作 class(这是一个单例)并执行操作,而不是在 viewcontroller 中使用 centralmanagerdelegates。
我做到了,效果很好。