ios - CoreBluetooth - 不发现服务

标签 ios objective-c iphone core-bluetooth

我在使用 CoreBluetooth 时遇到了一个有趣的问题。我使用 iPhone 6 Plus 和 iPhone 4S 进行测试。两个设备都在 8.2

我在类(class)的初始化方法中有以下设置:

 _centralManager = [[CBCentralManager alloc] initWithDelegate:self  
                                                        queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                                                      options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:YES]}];

 _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
                                                               queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                                                             options:@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:YES]}]; 

//My characteristic and service setup is here and I finally add them to the peripheral: 
[_peripheralManager addService:transferService];
[_peripheralManager addService:anotherService];

然后我开始外设管理广告和中央扫描如下:

[self.peripheralManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"My name",
                                           CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:my_UUID]] }];

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

此时委托(delegate)方法:

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

被调用,广告数据字典与我在 init 方法中执行的设置一致,但是当我到达 didDiscoverServices 时,我看不到外围设备的任何服务。我检查了控制台,指针是相同的(我将外围设备存储在一个属性中),UUID 也是如此。有趣的一点是,当我第一次发现外设时,名称是正确的,例如

<CBPeripheral: 0x1700f8500, identifier = xxxxx, name = My Name, state = disconnected>

但是当它到达 didDiscoverServices 时 - <CBPeripheral: 0x1700f8500, identifier =xxxx, name = iPhone, state = connected>

注意名称的不同。

我尝试将 peripheral 和 central 设置为不同的队列,但没有结果。我的问题是,是否可以同时使用外围设备和中央设备,这会在发现服务时造成问题吗?

编辑:另一个奇怪的地方。 1. 如果我重新启动两个设备,然后添加 sleep在调用广告和扫描之间调用 6 Plus 我可以看到 4S 的服务。 2. 如果我删除 sleep再次运行应用程序,我仍然可以看到该服务。但是,当我将 4S 插入 Xcode 并尝试从那里驱动流程时 - 我似乎无法获得它。 4S 的后续尝试也失败了。

最佳答案

与在外设完全初始化之前添加服务相关的类似问题。我发现在 peripheralManagerDidUpdateState: delegate 方法中添加服务是添加服务的好地方,因为它会在外围设备启动后立即调用。

关于ios - CoreBluetooth - 不发现服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29181040/

相关文章:

iphone - 根据仪器显示,WebView 似乎正在泄漏。通用Block-56

ios - 海量父子模式和委托(delegate)模式

ios - 单 View 应用程序具有无法更改的黑色背景

ios - 核心数据获取请求错误

c++ - Growl Notification - 不会使用框架 (Mist) 触发通知

ios - CoreData对象之间的关系

iphone - 如果您设置 'availability' 日期,您的 iPhone 应用程序是否会立即出现

PhP 在使用 iOS 图像进行方向校正后上传 ImageJPEG

ios - 如何使用 objective-c 在 iOS 中获取 UIImage 的阴影

ios - 删除了 NSUserDefaults