Ios BLE-didDiscoverServices从未被调用
我见过一些这种性质的问题,但没有一个能解决我的问题。我正在尝试连接到一个可扩展设备。在我回电话之前一切都很顺利Ios BLE-didDiscoverServices从未被调用,ios,objective-c,bluetooth-lowenergy,Ios,Objective C,Bluetooth Lowenergy,我见过一些这种性质的问题,但没有一个能解决我的问题。我正在尝试连接到一个可扩展设备。在我回电话之前一切都很顺利 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error 这似乎从未被称为。以下是我所拥有的: - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)per
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
这似乎从未被称为。以下是我所拥有的:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
self.connected = [NSString stringWithFormat:@"Connected: %@", peripheral.state == CBPeripheralStateConnected ? @"YES" : @"NO"];
NSLog(@"%@", self.connected);
[peripheral setDelegate:self];
if (peripheral.services) {
[self peripheral:peripheral didDiscoverServices:nil]; //already discovered services, DO NOT re-discover. Just pass along the peripheral.
} else {
[peripheral discoverServices:nil]; //yet to discover, normal path. Discover your services needed
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"here");
for(CBService* svc in peripheral.services)
{
if(svc.characteristics)
[self peripheral:peripheral didDiscoverCharacteristicsForService:svc error:nil]; //already discovered characteristic before, DO NOT do it again
else
[peripheral discoverCharacteristics:nil
forService:svc]; //need to discover characteristics
NSString *string = [NSString stringWithFormat:@"Discovered service: %@", svc.UUID];
NSLog(@"%@", string);
[self appendTextToDeviceInfo:string];
}
}
我可以看到日志输出:
已连接:是但就是这样。didDiscoverServices回调中似乎没有调用任何内容。我已经删除并重新安装了应用程序,还重新启动了设备 有人知道我做错了什么吗?谢谢大家! 编辑
以下是头文件:
#import <UIKit/UIKit.h>
@import CoreBluetooth;
@import QuartzCore;
@interface ViewController : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate>
@property (nonatomic, strong) CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheral *myPeripheral;
@end
您是否在
strong
属性中保存了对已连接外围设备的引用?是的,我保存了。我将把.h文件添加到帖子中。您将在哪里分配给self.myperipal
?我在didiscoverperipal
中进行分配。请参见编辑2。您的外围设备是什么?app store中的LightBlue应用程序能否发现其服务?
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if ([localName length] > 0) {
NSLog(@"Found the device: %@", localName);
[_centralManager cancelPeripheralConnection:peripheral]; //IMPORTANT, to clear off any pending connections
[_centralManager stopScan];
_myPeripheral = peripheral;
_myPeripheral.delegate = self;
[_centralManager connectPeripheral:_myPeripheral options:nil];
}
}