ios - 在 SWIFT 中断开 BLE 外设

标签 ios swift bluetooth bluetooth-lowenergy core-bluetooth

我在断开 Swift 中的 BLE 外设时遇到了一些问题。首先,我尝试只使用 cancelPeripheralConnection: 函数。但是,如果我只是调用此函数,则永远不会调用 didDisconnectPeripheral 函数。所以我试着关注Apple's引用指南。据说,您应该在断开连接之前删除所有通知。这真的有必要吗?是否有可能一步取消所有通知?我设置了很多通知,所以我必须搜索很多服务和特性来重置它们。我想,这不可能是一个“做得很好”的解决方案。

编辑: 好吧,我发现,如果我在我的 BluetoothManager 类中调用它,cancelPeripheralConnection 工作得很好,其中 CBCentralManagerCBPeripheralDelegate 包含在内...有没有办法断开与此功能之外的外围设备的连接?

编辑 4:

import UIKit

class ValueCollectionView: UICollectionViewController
{
    var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
    var bluetoothManager: BluetoothManager = BluetoothManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
        self.navigationItem.leftBarButtonItem = newBackButton;
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    func back(sender: UIBarButtonItem)
    {
        bluetoothManager.disconnectPeripheral(selectedPeripheralIndex!)
        self.navigationController?.popViewControllerAnimated(true)
    }
//Some Collection View functions...
}

这是我对 disconnectPeripheral 函数的实现(集成在 BluetoothManager 类中):

func disconnectPeripheral(peripheralIndex: Int)
{
    CBmanager.cancelPeripheralConnection(peripheralArray![peripheralIndex].peripheral)
}

但无论如何,如果我调用此函数,则不会调用 didDisconnectPeripheral 函数。当我将函数放在 BluetoothManager 类中时,例如在我发现最后一个特征后,一切正常。

编辑 5:

class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
{
    var CBmanager: CBCentralManager = CBCentralManager()

    override init()
    {
        super.init()
        self.CBmanager = CBCentralManager(delegate: self, queue: nil)
    }

    func connectPeripheral(peripheralIndex: Int)
    {
        CBmanager.connectPeripheral(peripheralArray![peripheralIndex].peripheral, options: nil)
    }

    func disconnectPeripheral(peripheralIndex: Int)
    {
        CBmanager.cancelPeripheralConnection(peripheralArray![peripheralIndex].peripheral)
    }

//The other CentralManager functions...

}

最佳答案

对于您的第一个疑问,是的,我们应该在断开与外围设备的连接之前取消注册订阅的特征,原因在 Apple Documentation 中给出。 :

Note: The cancelPeripheralConnection: method is nonblocking, and any CBPeripheral class commands that are still pending to the peripheral you’re trying to disconnect may or may not finish executing. Because other apps may still have a connection to the peripheral, canceling a local connection does not guarantee that the underlying physical link is immediately disconnected. From your app’s perspective, however, the peripheral is considered disconnected, and the central manager object calls the centralManager:didDisconnectPeripheral:error: method of its delegate object.

现在,回到你的另一个问题 -

Is there a way to disconnect to a peripheral outside of this function?

您需要断开它与您实例化并开始连接的实例的连接。只要您可以在同一个对象上调用取消,它就应该可以工作。

myCentralManager.cancelPeripheralConnection(peripheral)

在我的应用程序中,我不得不使用来自许多不同类的 BLE 功能,这导致我编写了一个单例 MyBLEManager 并且所有类都将进入这个单例以进行所有 BLE 相关事件。这笔交易效果很好,有助于仅限于一个类(class)的故障排除。您可以尝试一下。

关于ios - 在 SWIFT 中断开 BLE 外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263483/

相关文章:

ios - 在 iOS 应用程序中,如何检测用户按下了其外部键盘上的箭头键之一?

ios - 如何将适用于 iOS 的 InApp 购买和 native SDK 与 Haxe 集成

unit-testing - MagicalRecord = swift_dynamicCastClassUnconditional 仅在单元测试中

java - 将目标 SDK 从 28 更改为 29 导致 BLE 扫描不起作用

Swift 为 c 中定义的结构定义双指针

arrays - 需要为我的 CollectionView 获取字典中的数组计数

java - 如何共享fragment和activity之间的连接

ios - 在 iTunes 连接上更改应用程序名称

ios - 理解如何在 Swift 中初始化 Object-c 对象的问题

ios - 如何删除 IBACTION 中特定解析对象的行?