gpt4 book ai didi

objective-c - scanForPeripheralsWithServices :options: unable to connect when specifying services

转载 作者:太空狗 更新时间:2023-10-30 03:50:27 26 4
gpt4 key购买 nike

当使用 scanForPeripheralsWithServices:options 时,我能够在使用时发现服务

// Scanning with nil services will return all devices.
NSLog(@"Looking for any service.");
[self.centralManager scanForPeripheralsWithServices:nil options:nil];

但是,当使用通过以下代码从蓝牙设备获取的服务标识符预先指定服务时,我无法发现该设备。

#define DEVICE_INFO_SERVICE_UUID @"180a"
NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID], nil];

[self.centralManager scanForPeripheralsWithServices:services options:nil];

怎么了?

最佳答案

[-CBCentralManager scanForPeripheralsWithServices:options:] 仅返回主动通告它们支持特定服务的外围设备。

如果您正在寻找的服务没有被外围设备公布,您需要连接到它,然后发现您想要访问的服务。

由于广告数据包(在扫描时处理)中的空间有限,通常只有主要服务被广告。设备信息服务通常在大多数 BLE 外围设备上可用,因此通常不会公布。但是,一旦使用 [-CBPeripheral discoverServices:] 连接到外围设备,您就会看到此服务。


作为旁注,只是一个小的 Objective-C 提醒:

您可以使用初始化语法来减少代码的冗长:

[self.centralManager 
scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]]
options:nil];

关于objective-c - scanForPeripheralsWithServices :options: unable to connect when specifying services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22889321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com