如何通过蓝牙快速获取配对设备列表

时间:2019-02-19 11:16:06

标签: swift bluetooth

我正在研究一个示例项目,以通过蓝牙获取配对设备(iPhone和Andriod)的列表。下面的代码正在为我查找。但是未调用didDiscover方法。我的代码有什么问题。将提供任何帮助

代码:

transferServiceUUID = (CBUUID (string:"180D"))

func centralManagerDidUpdateState(_ central: CBCentralManager) {
        guard central.state  == .poweredOn else {
            return
        }
        scanDevice()
    }

    func scanDevice() {

        centralManager?.scanForPeripherals(
            withServices: [transferServiceUUID!], options: [
                CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: true as Bool)
            ]
        )

      }

    func didReadPeripheral(_ peripheral: CBPeripheral, rssi: NSNumber) {
        let name = peripheral.name

    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

        print("Discovered \(peripheral.name) at \(RSSI)")
       // if discoveredPeripheral != peripheral {
            discoveredPeripheral = [peripheral]
            print("Connecting to peripheral \(peripheral)")
            centralManager?.connect(peripheral, options: nil)
       // }
    }
    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
        print("Failed to connect to \(peripheral). (\(error!.localizedDescription))")

    }
    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

        centralManager?.stopScan()
        peripheral.delegate = self
        peripheral.discoverServices([transferServiceUUID!])
    }

0 个答案:

没有答案