在Swift中使用预定义的UUID发现蓝牙

问题描述 投票:0回答:1

我正在尝试构建一个可以在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在后台模式下扫描蓝牙设备吗?

提前感谢。

swift background core-bluetooth foreground bluetooth-gatt
1个回答
0
投票

CBUUID的格式应为

centralManager.scanForPeripherals(withServices: 
    [CBUUID(string: "180A"), CBUUID(string: "1801"), CBUUID(string: "1800"), CBUUID(string: "1812")],
    options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
© www.soinside.com 2019 - 2024. All rights reserved.