Objective c 扫描外围设备WithServices:选项:指定服务时无法连接

Objective c 扫描外围设备WithServices:选项:指定服务时无法连接,objective-c,ios7,bluetooth-lowenergy,core-bluetooth,Objective C,Ios7,Bluetooth Lowenergy,Core Bluetooth,使用ScanFor外围设备SwithServices:options时,我能够在使用时发现服务 // Scanning with nil services will return all devices. NSLog(@"Looking for any service."); [self.centralManager scanForPeripheralsWithServices:nil options:nil]; 但是,当使用通过以下代码从蓝牙设备获得的服务标识符提前指定服务时,我无法发现该设

使用ScanFor外围设备SwithServices: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];
怎么了?

此代码对我有效(当扫描带有服务@“feff”的设备时)

在这段代码中,我没有以nil结束,列表中只有一个服务

您是否正在检查中央管理器是否已通电

if (self->CM.state  != CBCentralManagerStatePoweredOn)
{
    printf("CoreBluetooth not correctly initialized !\n");
    printf("State = %d (%s)\r\n",
        self->CM.state,[self centralManagerStateToString:self.CM.state]);
    return -1;
}

[-CBCentralManager scanForPeripheralsWithServices:options:]仅返回主动宣传支持特定服务的外围设备

如果外围设备未公布您正在查找的服务,则需要连接到该服务,然后查找您希望访问的服务

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


作为旁注,只需一个次要的目标C提醒:

您可以使用初始值设定项语法来减少代码的冗长:

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

您的代码与原始代码有何不同?:-)对于实践中的第一个代码块,没有太多;-)我直接从我的项目中粘贴了代码,该代码用于指示缺少其他内容(如检查CBCentralManagerStatePoweredOn)。请欣赏初始值设定项语法。谢谢这是一个信息性的旁注,而不是作为判断的辅助。结果证明我的广告做得不对。这里的代码有效,但我修复了外围代码,一切正常。我不确定,我们如何得到这个值,“180a”?你怎么找到的?我有一个BT设备,需要连接到我的iOS应用程序。
[self.centralManager 
 scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]] 
                        options:nil];