Swift 3:无法通过BLE连接到外围设备 [英] Swift 3: Can't connect to peripheral via BLE
问题描述
我是使用BLE的新手,目前正在尝试制作一个简单的应用程序,该应用程序将连接到我的自定义BLE设备.我能够发现BLE设备,但是由于某种原因我无法连接到它. 我尝试使用浅蓝色"进行检查,它显示我的设备可连接,并且工作正常.但是,在我发现设备后,在我的应用程序中,CB管理器尝试连接到该设备,并且似乎冻结"了吗?永远不会触发功能"didConnect外设",而外设的状态则永远是正在连接".
I'm new to working with BLE, currently trying to make a simple application which would connect to my custom BLE device. I am able to discover the BLE device, but for some reason i can't connect to it. I tried to check it with 'Light Blue', it shows my device as connectable and seems to work fine. But in my app after i discover the device, CB manager tries to connect to it and seems to 'freeze'? Function 'didConnect peripheral' is never triggered, and state of peripheral is forever 'connecting'.
我如何识别问题?我可以在连接方法中包含任何选项,或者以某种方式跟踪连接过程吗?
How can i identify the problem? Is there any options i can include in connection method, or somehow track the connection process?
对于在哪里寻找问题的任何建议,我将不胜感激.
I would appreciate any advice where to search for problems.
使用Swift 3在XCode 8.2.1中工作.测试电话上安装了iOS 10.2.1
Working in XCode 8.2.1, using Swift 3. iOS 10.2.1 installed on the testing phone
这是我的代码:
import UIKit
import CoreBluetooth
class InfoPageViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var manager:CBCentralManager!
var peripheral:CBPeripheral!
let BEAN_NAME = "MyDevice"
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
let device = (advertisementData as NSDictionary)
.object(forKey: CBAdvertisementDataLocalNameKey)
as? NSString
if device?.contains(BEAN_NAME) == true {
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connect(peripheral, options: nil)
print("discovered \(BEAN_NAME)")
}
}
func centralManager(
central: CBCentralManager,
didConnect peripheral: CBPeripheral) {
print("connected to \(BEAN_NAME)")
peripheral.discoverServices(nil)
}
推荐答案
func centralManager(central: CBCentralManager, didConnect peripheral: CBPeripheral) {}
--------------------^
对:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
--------------------^
该方法的签名不正确,您错过了_
.
The signature of the method is not the correct one, you are missing the _
.
方法签名很重要.
我们可以假设,因为这些委托方法是可选的,所以在内部,Apple代码会自问:
我的代表是否已实现方法func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
(respondsToSelector:
)?在您的情况下,不是,因为不一样,因此不会调用您的.
Method signatures are important.
We can assume, since theses delegate methods are optional, that internally, the Apple code asks itself:
Does my delegate have the method func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
implemented (respondsToSelector:
)? In your case, not, because it's not the same, and then yours is not called.
您可以从文档中复制/粘贴该文档或将其删除,然后让XCode进行自动补全.
You copy/paste the one from the doc or remove it and let XCode do its autocompletion thing.
这篇关于Swift 3:无法通过BLE连接到外围设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!