我正在尝试构建一个可以在Swift中发现蓝牙设备的应用(前景应用)。当未提供服务uuid时,我可以找到所有可用的设备,例如以下代码:
centralManager.scanForPeripherals(withServices: nil,options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
当我指定预定义的UUID时,无法获得设备。例如,我尝试放入通用访问/设备信息的UUID。许多网络文章指出,它应该找到所有可用的设备。但是,我没有从该指令中获得任何设备。
centralManager.scanForPeripherals(withServices: [CBUUID(string: "0x180A"), CBUUID(string: "0x1801"), CBUUID(string: "0x1800"), CBUUID(string: "0x1812")],options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
应该在此指令中添加其他设置吗?
另外,我可以使用预定义的uuid在后台模式下扫描蓝牙设备吗?
提前感谢。
CBUUID
的格式应为
centralManager.scanForPeripherals(withServices:
[CBUUID(string: "180A"), CBUUID(string: "1801"), CBUUID(string: "1800"), CBUUID(string: "1812")],
options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])