简体   繁体   English

调用discoverServices后,CBPeripheral连接保持断开连接

[英]CBPeripheral connection keep disconnecting after calling discoverServices

I'm trying to integrate the coreBluetooth within my application. 我正在尝试将coreBluetooth集成到我的应用程序中。 Here is my code: 这是我的代码:

@interface Central() <CBCentralManagerDelegate>
@property (strong, nonatomic) CBPeripheral * activePeripheral;
@property (strong, nonatomic) CBCentralManager * centralManager;
@end

@implementation Central

- (id) init
{
     self = [super init];
     if (self)
     {
        NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @YES};
        self.centralManager = [[CBCentralManager alloc] initWithDelegate:self  queue:defaultGlobalQueue options:options];
     }
     return self;
}

- (void) startScanning:(NSInteger)timeout
{
    self.activePeripheral = nil;
    [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]] options:nil];
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    if (self.activePeripheral != nil)
    {
        return;
    }
    self.activePeripheral = peripheral;
    [self.centralManager connectPeripheral:peripheral options:nil];

}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]]];
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
//    dispatch_async(dispatch_get_main_queue(), ^{
    NSLog(@"Disconnected from peripheral : %@",peripheral);
    if (error)
    {
        NSLog(@"Error disconnecting peripheral: %@", error.localizedDescription);
        //[self didErrorDelegateWithPeripheral:peripheral andError:error andCode:BLE_FAILED_TO_CONNECT];
    }
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    //Logic here
}

I'm calling startScanning within a viewcontroller and it always enters in the (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error callback after calling [self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]]]; 我是一个内调用startScanning viewcontroller ,它始终在进入(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error调用后回调[self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]]];

here is the error I'm always seeing: 这是我一直看到的错误:

Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo=0x175b8810 {NSLocalizedDescription=The specified device has disconnected from us.}

Anyone knows what I'm missing? 谁知道我错过了什么?

Thanks for your help. 谢谢你的帮助。

I finally figured out the problem, we need to set the delegate of the peripheral discovered before trying to discover the services 我终于找到了问题,我们需要在尝试发现服务之前设置发现的外围设备的委托

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
peripheral.delegate = self;
    [self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM