CoreBluetooth - 读/写的长数据

时间:2015-11-18 19:42:57

标签: ios objective-c core-bluetooth

我正在尝试通过readValueForCharacteristic发送一个长数据(~2150字节),如下所示:

我的特点:

    self.centralInfoCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:WRITE_CENTRAL_INFO_UUID] properties:CBCharacteristicPropertyWriteWithoutResponse value:nil permissions:CBAttributePermissionsWriteable];
    self.peripheralInfoCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:READ_INFO_PERIPHERAL_UUID] properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];

中央要求特征值:

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
        if (error) {
            NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
            return;
        }

        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:READ_INFO_PERIPHERAL_UUID]]){
                [self.discoveredPeripheral readValueForCharacteristic:characteristic];
            } else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:WRITE_CENTRAL_INFO_UUID]]){
                [self.discoveredPeripheral writeValue:[self.myUserInfo transformJsonData] forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
            }
        }
    }

当中心正在请求628字节时,这个过程就像一个魅力!但是,当它请求2150字节时,接下来的步骤发生:

on peripheral :(此方法被调用4次,其中offset = {0,157,314,471})

    - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{
      if ([request.characteristic.UUID.UUIDString isEqualToString:READ_INFO_PERIPHERAL_UUID]){    
          if (request.offset > peripheralInfoCharacteristicData.length) {
              [peripheral respondToRequest:request withResult:CBATTErrorInvalidOffset];
              return;
          }
          request.value = [peripheralInfoCharacteristicData subdataWithRange:NSMakeRange(request.offset, peripheralInfoCharacteristicData.length - request.offset)];
          [self.peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];   
      }

在中央之后:

   - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
      if ([characteristic.UUID.UUIDString isEqualToString:READ_INFO_PERIPHERAL_UUID]){
          // characteristic.value is equal 628. It is wrong! Should be 2150
          // So I can't deserialize the NSData
      }
    }

那么,出了什么问题?在readValueCharacteristic上传递> 628字节是不可能的?

当我使用writeValueCharacteristic时,不会调用didReceiveWriteRequests,就在我传递628个字节时。

peripheralInfoCharacteristicData变量在viewDidLoad上实例化:

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
                                                                                  @"ID": self.userId,
                                                                                  @"U": self.userNameStr
                                                                                  }];
    if (self.userPhotoImg) [dictionary setObject:[self imageToNSString:self.userPhotoImg] forKey:@"F"];
    peripheralInfoCharacteristicData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
   // peripheralInfoCharacteristicData.length = 2150

0 个答案:

没有答案
相关问题