通过蓝牙4.0 LE发送图像文件

tag*_*bek 13 bluetooth objective-c ios core-bluetooth

我试图通过蓝牙4.0 LE将一个.png图像文件从一个iOS设备发送到另一个.

我能够像字符串一样简单的数据,但无法成功发送和利用图像文件.

在外围设备中,我从这开始

pictureBeforeData = [UIImage imageNamed:@"myImage.png"];
NSData *myData = UIImagePNGRepresentation(pictureBeforeData);
Run Code Online (Sandbox Code Playgroud)

然后我创造myData了一个特征的价值.

_myCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:_myCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value:myData
                                  permissions:CBAttributePermissionsReadable];
Run Code Online (Sandbox Code Playgroud)

在中央设备中,我会在更新特征值时使用此功能

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
  if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:_myCharacteristicUUID]]) {
    NSLog(@"PICTURE CHARACTERISTIC FOUND"); // This successfully gets logged

    NSData *dataFromCharacteristic = [[NSData alloc] initWithData:characteristic.value];

    UIImage *imageFromData = [[UIImage alloc]initWithData:dataFromCharacteristic];

    // This correctly logs the size of my image
    NSLog(@"SIZE: %f, %f", imageFromData.size.height, imageFromData.size.width);

    // This causes the imageView to turn completely black
    _sentPictureImageView.image = imageFromData;

    if (!([_myImagesArray containsObject:imageFromData]))
    {
      NSLog(@"DOES NOT CONTAIN"); // This is successfully logged
      [_myImagesArray addObject:imageFromData]; // This runs but is apparently not adding the image to the array
    }

    NSLog(@"COUNT: %u", _contactsImagesArray.count); // This always logs 0 - The array is empty }
Run Code Online (Sandbox Code Playgroud)

编辑8-27-13:我能够成功发送单个颜色1by1像素.png文件,该文件大约5,600字节.我无法发送一个只有大约2,000字节的多色20by20pixel .png文件.我能够发送只包含一种颜色和更少像素的较大文件,但无法发送包含许多颜色和更多像素的较小文件.

编辑8-30-13:我必须将数据解析为较小的块.在Apple的一个示例项目中,我发现了一种方法.我似乎无法理解如何在我自己的代码中实现这一点,但我目前正试图弄清楚如何.这是代码:

- (void)sendData {
// First up, check if we're meant to be sending an EOM
static BOOL sendingEOM = NO;

if (sendingEOM) {

    // send it
    BOOL didSend = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

    // Did it send?
    if (didSend) {

        // It did, so mark it as sent
        sendingEOM = NO;

        NSLog(@"Sent: EOM");
    }

    // It didn't send, so we'll exit and wait for peripheralManagerIsReadyToUpdateSubscribers to call sendData again
    return;
}

// We're not sending an EOM, so we're sending data

// Is there any left to send?

if (self.sendDataIndex >= self.dataToSend.length) {

    // No data left.  Do nothing
    return;
}

// There's data left, so send until the callback fails, or we're done.

BOOL didSend = YES;

while (didSend) {

    // Make the next chunk

    // Work out how big it should be
    NSInteger amountToSend = self.dataToSend.length - self.sendDataIndex;

    // Can't be longer than 20 bytes
    if (amountToSend > NOTIFY_MTU) amountToSend = NOTIFY_MTU;

    // Copy out the data we want
    NSData *chunk = [NSData dataWithBytes:self.dataToSend.bytes+self.sendDataIndex length:amountToSend];

    // Send it
    didSend = [self.peripheralManager updateValue:chunk forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

    // If it didn't work, drop out and wait for the callback
    if (!didSend) {
        return;
    }

    NSString *stringFromData = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding];
    NSLog(@"Sent: %@", stringFromData);

    // It did send, so update our index
    self.sendDataIndex += amountToSend;

    // Was it the last one?
    if (self.sendDataIndex >= self.dataToSend.length) {

        // It was - send an EOM

        // Set this so if the send fails, we'll send it next time
        sendingEOM = YES;

        // Send it
        BOOL eomSent = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil];

        if (eomSent) {
            // It sent, we're all done
            sendingEOM = NO;

            NSLog(@"Sent: EOM");
        }

        return;
    }
}}
Run Code Online (Sandbox Code Playgroud)

imageView是黑色方块,应该显示我发送的图像. 在此输入图像描述

all*_*rog 11

在iOS 6上,BLE允许您发送大约20个字节的数据块.有关如何切片要发送的数据以及如何使用基于通知的传输的示例,请下载BTLE Transfer Apple示例应用程序.

为了更好地了解您可以达到的速度,我建议您分析此图表http://www.scriptreactor.com/conn_interval-throughput.pdf它显示可实现的速度作为连接间隔的函数.iOS不会为您提供如此精细的控制,但此信息对您来说仍然很有价值.

  • @tagabek做了这个或其他答案解决了你的问题?如果您的问题得到解答,则应接受正确的解决方案.您可以使用答案上的复选标记来完成. (2认同)

Wai*_*ain 2

BTLE 在特征和任何数据包中可发送的数据量方面受到显着限制。您应该在设备之间建立连接,然后分割图像数据并发送多个小数据包。2013 WWDC 视频将为您提供很好的概述和代码示例。