2013-07-31 32 views
10

Trong dự án gần đây của tôi, tôi cần giao tiếp phần cứng (Bluetooth Low energy) .Tôi đã triển khai tất cả mã phương thức đại biểu. Tôi có thể Kết nối phần cứng và thiết bị, Nhưng tôi không nhận được cảnh báo ghép nối (Ảnh chụp màn hình được đính kèm). Tại sao nó không yêu cầu ghép nối? Cảm ơn bạn.iOS Core Bluetooth Không yêu cầu cặp

#import "BTWCentralConnectionManager.h" 

    @implementation BTWCentralConnectionManager 

    @synthesize cbcManager; 

    @synthesize discoveredPeripheral; 

    @synthesize findMeServiceCharacteristic; 

    @synthesize findMeService; 

    @synthesize delegate=_delegate; 

    static NSString *[email protected]"1802"; 

    static NSString *[email protected]"2A06"; 

    static BTWCentralConnectionManager* connectionManager = nil; 

    +(BTWCentralConnectionManager *)sharedConnectionManager{ 

    @synchronized(self) 

    { 

     if (!connectionManager){ 

      connectionManager=[[self alloc] init]; 


     } 

     return connectionManager; 

    } 

    return nil; 

} 


    -(void)findMe { 

    Byte code=0x02; 

    if(self.discoveredPeripheral){ 

     [self.discoveredPeripheral writeValue:[NSData dataWithBytes:&code length:1] forCharacteristic:self.findMeServiceCharacteristic type:CBCharacteristicWriteWithoutResponse]; 


    }else{ 

     UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Invalid Charactersitcs" delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

     [alertView show]; 

     alertView=nil; 

    } 
} 

-(void)searchForDevices{ 

    self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil]; 

} 

    -(void)connect { 

    NSDictionary* connectOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]; 


    [self.cbcManager connectPeripheral:self.discoveredPeripheral options:connectOptions]; 

} 

    -(void)disconnect{ 

    [self cleanup]; 

} 


- (void)centralManagerDidUpdateState:(CBCentralManager *)central { 

    switch (central.state) { 

     case CBCentralManagerStatePoweredOn:{ 

      [self.cbcManager scanForPeripheralsWithServices:@[ [CBUUID UUIDWithString:kFindMeServiceUUID] ] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }]; 

     } 

      break; 

      // Scans for any peripheral 

     default:{ 

      UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Cental Manager did change state" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 

      [alertView show]; 

      alertView=nil; 

     } 

      break; 
    } 

} 


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

    // Stops scanning for peripheral 

    [self.cbcManager stopScan]; 


    if (self.discoveredPeripheral != peripheral) { 

     self.discoveredPeripheral = peripheral; 

     [self.delegate didDeviceDiscoverd:self.discoveredPeripheral.name]; 

    } 

} 

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error 
{ 

    [self.delegate didDeviceConnectionFailed:error]; 

    [self cleanup]; 
} 


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral 
{ 

    [self.delegate didDeviceConnected]; 

    [self.discoveredPeripheral setDelegate:self]; 

    [self.discoveredPeripheral discoverServices:@[[CBUUID UUIDWithString:kFindMeServiceUUID]]]; 

} 


- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error { 

    if (error) { 

     NSString *strMsg=[NSString stringWithFormat:@"didDiscoverServices: %@", error]; 

     UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg 
delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

     [alertView show]; 

     alertView=nil; 

     [self cleanup]; 

     return; 

    } 

    for (CBService *service in aPeripheral.services) { 

     if ([service.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) { 

      self.findMeService=service; 

      [self.discoveredPeripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kFindMeCharacteristicUUID]] forService:self.findMeService]; 

     } 

    } 

} 

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error 
{ 

    if(error){ 

     NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@", error]; 

     UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

     [alertView show]; 

     alertView=nil; 

    } 

    for(CBCharacteristic *character in [service characteristics]) 
    { 

     if([[service UUID] isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]] && 
      [[character UUID] isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]]) 
     { 

      NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@", character]; 

      UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

      [alertView show]; 

      alertView=nil; 

      self.findMeServiceCharacteristic = character; 

     } 
    } 
} 


- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error 

{ 

    NSString *strMsg=[NSString stringWithFormat:@"Did update value for characteristic %@, new value: %@, error: %@", characteristic, [characteristic value], error]; 

    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

    [alertView show]; 

    alertView=nil; 


} 

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { 

    if (error) { 

     NSLog(@"Error changing notification state: %@", error.localizedDescription); 

    } 


    // Exits if it's not the transfer characteristic 

    if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]]) { 

     return; 

    } 


    NSString *strMsg=[NSString stringWithFormat:@"didUpdateNotificationStateForCharacteristic %@, reason: %@", characteristic, error]; 

    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

    [alertView show]; 


    alertView=nil; 

} 

- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error 

{ 

    if (error) 

    { 

     NSString *strMsg=[NSString stringWithFormat:@"Failed to write value for characteristic %@, reason: %@", characteristic, error]; 

     UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

     [alertView show]; 

     alertView=nil; 

    } 
    else 
    { 
     NSString *strMsg=[NSString stringWithFormat:@"Did write value for characterstic %@, new value: %@", characteristic, [characteristic value]]; 

     UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil  otherButtonTitles:@"OK", nil]; 

     [alertView show]; 

     alertView=nil; 

    } 
} 

- (void)cleanup 

{ 

    if (!self.discoveredPeripheral.isConnected) { 

     return; 

    } 

    if (self.discoveredPeripheral.services != nil) { 

     for (CBService *service in self.discoveredPeripheral.services) { 

      if (service.characteristics != nil) { 

       for (CBCharacteristic *characteristic in service.characteristics) { 

        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) { 

         if (characteristic.isNotifying) { 

          [self.discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic]; 

          return; 

         } 

        } 

       } 

      } 
     } 
    } 
    [self.cbcManager cancelPeripheralConnection:self.discoveredPeripheral]; 
    [self.delegate didDeviceDisconnected]; 
} 
@end 
` 
+0

mà không cần mã nguồn, rất khó để giúp bạn. – Bloodcount

+0

Cảm ơn Bloodcount, Vui lòng kiểm tra câu hỏi đã cập nhật. – Mangesh

+0

Bạn đã thử với thiết bị iOS6 chưa? – Larme

Trả lời

16

Nếu tôi hiểu bạn đúng bạn có thể viết một giá trị thành công cho một đặc trưng nhưng bạn không nhận được một yêu cầu ghép nối.

Việc ghép nối được kích hoạt bởi thiết bị ngoại vi. Có nghĩa là thiết bị ngoại vi phải từ chối yêu cầu viết hoặc đọc của trung tâm của bạn đến một đặc tính. Trung tâm của bạn bị từ chối "xác thực trái phép" và sau đó cố gắng ghép nối với thiết bị ngoại vi và hiển thị cảnh báo ghép nối bật lên mà bạn đang đợi. Đây là tất cả được thực hiện bởi bluetooth lõi tự động. Điều duy nhất bạn cần làm là thay đổi các tùy chọn đặc điểm và quyền trong thiết bị ngoại vi của bạn. Đây là mã mẫu táo để kích hoạt một kết nối:

emailCharacteristic = [[CBMutableCharacteristic alloc] 
    initWithType:emailCharacteristicUUID 
    properties:CBCharacteristicPropertyRead 
    | CBCharacteristicPropertyNotifyEncryptionRequired 
    value:nil permissions:CBAttributePermissionsReadEncryptionRequired]; 

nguồn: CoreBluetooth _concepts iOS7 preview

Ngoài ra kiểm tra WWDC 2012 advanced core bluetooth video ở 28 phút họ giải thích các khái niệm về ghép nối.

+1

Đây là liên kết tới video: http://adcdownload.apple.com//videos/wwdc_2012__hd/session_705__advanced_core_bluetooth.mov – ThomasW

Các vấn đề liên quan