gpt4 book ai didi

ios - 在 SWIFT 中断开 BLE 外设

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:16 25 4
gpt4 key购买 nike

我在断开 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com