swift - 外围设备未连接 - Swift

标签 swift core-bluetooth bluetooth-lowenergy cbperipheral cbperipheralmanager

在我的 Swift 应用程序中,我有一个 View 可以找到蓝牙设备并连接到它。蓝牙设备已开机。我能够扫描并找到该设备,之后我调用该功能连接到它,但我没有得到任何反馈。我不确定为什么无法连接。

didFailToConnectPeripheral 或 didConnectPeripheral 均未返回任何值。

我如何让它工作?

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager: CBCentralManager!


    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CBCentralManager (delegate: self, queue: nil)
    }


    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

        print("Peripheral: \(peripheral)")

        manager.connectPeripheral(peripheral, options:nil)

        manager.stopScan()

    }



    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("connected!")
    }

    func centralManager(central: CBCentralManager, didDisconnectPeripheral
        peripheral: CBPeripheral, error: NSError?) {
            print("disconnected!")
    }


    func centralManager(central: CBCentralManager,
        didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
            print("failed")
    }


    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.scanForPeripheralsWithServices(nil, options: nil)
        default:
            print("default state")
        }
    }
}

最佳答案

您需要保留您尝试连接的 CBPeripheral 对象,否则一旦委托(delegate)方法 didDiscoverPeripheral 返回,它就会被释放。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager: CBCentralManager!
    var connectingPeripheral: CBPeripheral?


    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CBCentralManager (delegate: self, queue: nil)
    }


    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

        print("Peripheral: \(peripheral)")

        self.connectingPeripheral=peripheral    

        manager.connectPeripheral(peripheral, options:nil)

        manager.stopScan()
    }

...

}

关于swift - 外围设备未连接 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35026093/

相关文章:

iOS 使用 xib + 全局方法覆盖 UIView (Swift)

android - 在 Android 5.0 上连续扫描 BLE 广告是否可以接受?

javascript - 在 swiftui 中评估 JavaScript

ios - 如何限制用户使用返回的行数?

ios - 在 iOS 蓝牙外设上查找 TxPower

objective-c - 从 Mac 发现 RN4020 BLE 服务

java - Android,如何正确扫描和过滤 ble UUID

ios - 蓝牙配对请求连接

ios - 如何将此 Web 服务方法声明为全局方法?

iOS 核心蓝牙 : Advertised service is not discovered on peripheral