我正在尝试使用CoreBluetooth框架获取最近的蓝牙,我导入框架并实施CBCentralManagerDelegate
,CBPeripheralDelegate
。
这是我的代码:
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])
}
为什么我没有获得最近的设备列表?