scanForPeripheralsWithServices:options:指定服务时无法连接

Vis*_*ani 5 objective-c core-bluetooth bluetooth-lowenergy ios7

使用scanForPeripheralsWithServices:选项时,我可以在使用时发现服务

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

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

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

[self.centralManager scanForPeripheralsWithServices:services options:nil];
Run Code Online (Sandbox Code Playgroud)

怎么了?

Eta*_*tan 7

[-CBCentralManager scanForPeripheralsWithServices:options:]仅返回主动宣传它们支持某项服务的外围设备.

如果外围设备未公布您要查找的服务,则需要连接到该服务,然后发现您要访问的服务.

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


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

您可以使用初始化程序语法来减少代码的详细程度:

[self.centralManager 
 scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]] 
                        options:nil];
Run Code Online (Sandbox Code Playgroud)