iOS核心蓝牙:从中央发送数据,从外围设备获取响应



我正在开发一个蓝牙应用程序。我已经连接到蓝牙设备,并在其中找到了服务和特性。我面临着从中央发送写入请求和从外围接收响应的问题。我已经写了如下代码来写数据。我怎么知道外围设备收到了数据?我可以在外设中显示任何警报吗。同样,我想从外围设备接收数据,这样中央设备就应该显示一个警报,表明它已经从外围设备得到了响应。

下面是我写的代码

  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:  
  (CBService *)service error:(NSError *)error {
   if (!error) {
    printf("Characteristics of service with UUID : %s foundrn",[self   
   CBUUIDToString:service.UUID]);
    for(int i=0; i < service.characteristics.count; i++) {
        CBCharacteristic *c = [service.characteristics objectAtIndex:i];
        printf("Found characteristic %srn",[ self CBUUIDToString:c.UUID]);
        CBService *s = [peripheral.services objectAtIndex:(peripheral.services.count - 1)];
        if([self compareCBUUID:service.UUID UUID2:s.UUID])
        {
            printf("Finished discovering characteristics");
            break;
        }
     }
  }
   else {
    printf("Characteristic discorvery unsuccessfull !rn");
  }
  }

用于写入值

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:  
   (CBCharacteristic *)characteristic error:(NSError *)error
  {
    NSString *payloadMessage = @"Hello";
    NSData *payload = [payloadMessage dataUsingEncoding:NSUTF8StringEncoding];
    [peripheral writeValue:payload forCharacteristic:characteristic    
   type:CBCharacteristicWriteWithResponse];
 }

并且我需要获得ble设备的版本,即get_version=0x4a;请在这方面帮助我,或者提供从这两个设备写入和读取数据的任何示例。提前感谢

蓝牙中心可以通过两种方式从外围设备接收数据-

  1. 它可以对特性发出明确的读取请求
  2. 它可以订阅关于某个特征的通知——在这种情况下,当值发生变化或这些是新数据时,外围设备将通知中心。这更有效,因为它避免了中心不得不不断地轮询外围设备以寻找可能没有改变的数据

无论您使用哪种方法,数据都将通过对didUpdateValueForCharacteristic:委托方法的调用传递。

您似乎对这个方法的目的有点困惑,因为您的代码在那里发出了一个写请求,这可能不是您想要的。

当涉及到写入数据时,您可以在有响应或无响应的情况下写入数据。您的代码指定了响应。在这种情况下,当外围设备确认写入时,会调用didWriteValueForCharacteristic委托方法——这就是您知道数据已被接收的方式。

实现您在问题中描述的行为的一些简单代码是:

 -(void)sendData:(NSData *)data {
      [self.connectedPeripheral writeValue:data forCharacteristic:self.someCharacteristic type:CBCharacteristicWriteWithResponse];
 }

- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
             error:(NSError *)error {
         if (error == nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrote characteristic" message:@"Successfully wrote characteristic" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
            [alert show];
         }
         else {
             NSLog(@"Error writing characteristic %@",error);
         }
     }
  -(void)readCharacteristic {
    [self.connectedPeripheral readCharacteristic:self.someOtherCharacteristic];
}


- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {
     if (error == nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Read characteristic" message:[NSString stringWithFormat:@"Successfully read characteristic %@",characteristic.value] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok"];
            [alert show];
         }
         else {
             NSLog(@"Error reading characteristic %@",error);
         }
     }

最新更新