didDiscoverPeripheral从未在Swift中调用过

时间:2016-12-01 08:43:58

标签: swift macos bluetooth core-bluetooth

我在El Capitan上有以下非常简单的代码片段,它基本上是扫描BLE外围设备并连接到我想要的那个。然而,didDiscoverPeripheral永远不会被调用!它开始扫描但从未找到任何外围设备。对未来澄清要求的一些答案:

  1. 我在附近有BLE设备做广告。例如,我可以通过电话看到它们。

  2. 代码检查BLE是否已通电。

  3. 下面有很多类似的问题,但是没有一个问题对我有帮助。

  4. Question 1

    Question 2

    Question 3

    Question 4

    代码如下:

    import Cocoa
    import CoreBluetooth
    
    class ViewController: NSViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
    
    var centralManager: CBCentralManager!
    var peripheral: CBPeripheral!
    let DEVICE_NAME = "something"
    var currentCharacteristic: CBCharacteristic! = nil
    
    // Put CentralManager in the main queue
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.startManager()
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.startManager()
        // Do any additional setup after loading the view.
    }
    
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
    
    func startManager(){
        centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
    }
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch (central.state)
        {
        case CBCentralManagerState.unsupported:
            print("BLE Unsupported")
            break
        case CBCentralManagerState.unauthorized:
            print("BLE unauthorized")
            break
        case CBCentralManagerState.poweredOff:
            print("BLE is not on.")
            break
        case CBCentralManagerState.poweredOn:
            print("BLE is on.")
            print("Scanning...")
            self.centralManager?.scanForPeripherals(withServices: nil, options: nil)
            break
        case CBCentralManagerState.unknown:
            print("BLE state unknown")
            break
        default:
            print("BLE state default.")
        }
    }
    
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        // This is NEVER called!
        print("Peripheral: \(peripheral)")
        if (peripheral.name != nil && peripheral.name! == DEVICE_NAME){
            self.peripheral = peripheral
            self.centralManager.connect(self.peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
        }
    }
    
    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        peripheral.delegate = self
        peripheral.discoverServices(nil)
        print("Connected to \(peripheral)")
        self.stopScan()
    }
    
    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?){
        print("connection failed", error)
    }
    
    func stopScan(){
        self.centralManager.stopScan()
    }
    
    
    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?){
        if self.peripheral != nil {
            self.peripheral.delegate = nil
            self.peripheral = nil
        }
        print("Disconnected.", error)
        self.startManager()
    }
    }
    

    知道什么是错的吗?

0 个答案:

没有答案
相关问题