如何在Swift 3.0中最近的蓝牙设备列表

时间:2017-06-21 18:23:06

标签: swift swift3 bluetooth core-bluetooth cbcentralmanager

我正在尝试使用CoreBluetooth框架获取最近的蓝牙,我导入框架并实施CBCentralManagerDelegateCBPeripheralDelegate

这是我的代码:

import CoreBluetooth

        centralManager = CBCentralManager(delegate: self, queue: nil)

    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 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) {
        print("Peripheral Connected")

        // Stop scanning
        centralManager?.stopScan()
        print("Scanning stopped")

        // Clear the data that we may already have
        data.length = 0

        // Make sure we get the discovery callbacks
        peripheral.delegate = self

        // Search only for services that match our UUID
        peripheral.discoverServices([transferServiceUUID])
    }

为什么我没有获得最近的设备列表?

0 个答案:

没有答案