iOS 上的 scanForPeripheralsWithServices 未按服务 UUID 查找设备

scanForPeripheralsWithServices on iOS not finding devices by service UUID

我正在使用 scanForPeripheralsWithServices 通过服务 UUID 扫描设备。当我使用一些标准服务 UUID 值时,它有时会起作用。

当我尝试使用自定义服务 UUID 时,它通常找不到任何设备,但如果我将 nil 用于服务 UUID 列表,它会找到那些在我使用他们的服务 UUID 时找不到的相同设备.

这是怎么回事?难道是因为设备的广告包只能广告它的部分服务?

外围设备宣传的服务集通常小于它提供的服务。这是因为广告中的 space 有限,因此仅广告 "primary" 服务是有意义的。

来自Apple Core Bluetooth Programming Guide-

There are also limits as to how much space you can use when advertising data. When your app is in the foreground, it can use up to 28 bytes of space in the initial advertisement data for any combination of the two supported advertising data keys. If this space is used up, there are an additional 10 bytes of space in the scan response that can be used only for the local name. Any service UUIDs that do not fit in the allotted space are added to a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them. While your app is in the background, the local name is not advertised and all service UUIDs are place in the overflow area.

To help you stay within these space constraints, limit the service UUIDs you advertise to those that identify your primary services.

例如,心率设备可能提供心率服务、设备信息服务、电池服务等,但它只会宣传心率服务,因为这是其他设备将要提供的服务"interested in" 因为它将此设备与其他 类 设备区分开来。

识别设备并建立连接后,如果需要,可以发现其他服务。