2014-07-07 4 views
4

내 응용 프로그램 내에 coreBluetooth를 통합하려고합니다. 내가 항상보고 있어요 오류입니다CBPeripheral 연결은 discoverServices를 호출 한 후 계속 연결 해제합니다.

@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 
} 

나는 viewcontroller 내 startScanning를 호출하고있어 항상 여기

[self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"A65eBB2D-3D30-4981-9DB2-1996D88118A0"]]];를 호출 한 후 (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 콜백 입력 :

Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo=0x175b8810 {NSLocalizedDescription=The specified device has disconnected from us.} 
여기 내 코드입니다

누구나 내가 누락 된 부분을 알고 있습니까?

도움 주셔서 감사합니다. 나는 마침내 문제를 파악

답변

9

, 우리는 서비스

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

이는 방식으로 CoreBluetooth이 관련되지를 발견하기 전에 발견 된 주변의 대리자를 설정해야합니다. 처리해야하는 작업을 수행하기 전에 항상 이벤트 처리기를 연결하십시오. 다른 언어에서도 마찬가지입니다. – Etan