简体   繁体   English

Swift 3:无法通过BLE连接到外设

[英]Swift 3: Can't connect to peripheral via BLE

I'm new to working with BLE, currently trying to make a simple application which would connect to my custom BLE device. 我刚接触BLE,目前正在尝试制作一个简单的应用程序,它将连接到我的自定义BLE设备。 I am able to discover the BLE device, but for some reason i can't connect to it. 我能够发现BLE设备,但由于某种原因我无法连接到它。 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'? 但在我发现设备后,我的应用程序,CB经理尝试连接到它,似乎“冻结”? Function 'didConnect peripheral' is never triggered, and state of peripheral is forever 'connecting'. 功能'didConnect peripheral'永远不会被触发,外设状态永远是'连接'。

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. 我很感激任何建议在哪里寻找问题。

Working in XCode 8.2.1, using Swift 3. iOS 10.2.1 installed on the testing phone 在XCode 8.2.1中工作,使用Swift 3. iOS 10.2.1安装在测试手机上

Here's my code: 这是我的代码:

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) {}
--------------------^

Versus: 与:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
--------------------^

The signature of the method is not the correct one, you are missing the _ . 方法的签名不正确,你错过了_

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: )? 我们可以假设,因为这些委托方法是可选的,在内部,Apple代码会问自己:我的委托是否有方法func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)实现( respondsToSelector: func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) In your case, not, because it's not the same, and then yours is not called. 在你的情况下,不是,因为它不一样,然后你的不被调用。

You copy/paste the one from the doc or remove it and let XCode do its autocompletion thing. 您从文档中复制/粘贴一个或删除它,让XCode执行自动完成操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM