繁体   English   中英

iOS CoreBluetooth:从中央发送数据并从外围设备获取响应

[英]iOS CoreBluetooth:Sending data from central and getting response from peripheral

我正在开发蓝牙应用程序。 我已连接到蓝牙设备,并在其中找到了服务和特性。 我在从中央发送写请求并从外围设备接收响应时遇到问题。 我写了下面的代码来写数据。 我怎么知道外围设备收到了这些数据? 我可以在外围设备显示任何警报吗? 并且,以同样的方式,我想从外围设备接收数据,以便中心应显示警报,表明它已收到外围设备的响应。

这是我编写的以下代码

  - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:  
  (CBService *)service error:(NSError *)error {
   if (!error) {
    printf("Characteristics of service with UUID : %s found\r\n",[self   
   CBUUIDToString:service.UUID]);
    for(int i=0; i < service.characteristics.count; i++) {
        CBCharacteristic *c = [service.characteristics objectAtIndex:i];
        printf("Found characteristic %s\r\n",[ 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 !\r\n");
  }

  }

用于写值

- (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);
         }

     }

暂无
暂无

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

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