如何在Swift 3.0中获取最近的蓝牙设备列表
我正在尝试使用框架获取最近的蓝牙,我导入框架并实现如何在Swift 3.0中获取最近的蓝牙设备列表,swift,swift3,bluetooth,core-bluetooth,cbcentralmanager,Swift,Swift3,Bluetooth,Core Bluetooth,Cbcentralmanager,我正在尝试使用框架获取最近的蓝牙,我导入框架并实现CBCentralManagerDelegate,cbperipheraldegate 这是我的密码: import CoreBluetooth centralManager = CBCentralManager(delegate: self, queue: nil) func centralManagerDidUpdateState(_ central: CBCentralManager) { gua
CBCentralManagerDelegate
,cbperipheraldegate
这是我的密码:
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])
}
为什么我没有得到最近的设备列表?您确定您的外设正在使用
transferServiceUUID
为服务做广告吗?尝试使用这些库并将它们添加到您的应用程序中。实际上,我并没有接近测试设备。我正在使用苹果指南来解决这个问题。您能帮我一个忙吗?请先试用centralManager?。扫描外围设备(服务:无,选项:相同)
@Larme我得到一些设备列表。我不确定他们是否正确。甚至我也无法得到设备的名称。