蓝牙4.0 LE发送图片文件 [英] Sending image file over Bluetooth 4.0 LE

查看:264
本文介绍了蓝牙4.0 LE发送图片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个PNG图片文件通过蓝牙发送4.0从一个iOS设备到另一个LE。

I am trying to send an .png image file from one iOS Device to another over Bluetooth 4.0 LE.

我能够像串简单的数据块,但无法成功发送和使用图像文件。

I am able to simple pieces of data like strings, but unable to successfully send and utilize image files.

在外围设备,我开始了这个

pictureBeforeData = [UIImage imageNamed:@"myImage.png"];
NSData *myData = UIImagePNGRepresentation(pictureBeforeData);

然后我让 myData的 A的特征值。

_myCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:_myCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value:myData
                                  permissions:CBAttributePermissionsReadable];

在中央设备,我有这个在特征值更新

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

修改13年8月27日:我能够成功地发送了一个单一的颜色1by1所在像素.png文件的文件大约5600字节大。我无法发送多色20by20pixel .png文件也就是只有2000字节。我能够在只包含一个颜色和更少的像素较大的文件发送,但无法发送包含许多颜色和更多的像素较小的文件。

EDIT 8-27-13: I am able to send over a single color 1by1 pixel .png file successfully that file is about 5,600 bytes large. I am unable to send a multi-color 20by20pixel .png file that is only about 2,000 bytes. I am able to send over the larger file that contains only one color and less pixels, but unable to send the smaller file that contains many colors and more pixels.

修改13年8月30日:我绝对有数据解析成小块。在苹果公司的例子项目之一,我发现了这一点的方法。我似乎无法确切地了解如何实现这在我自己的code,但我目前正在试图弄清楚如何。这里的code:

EDIT 8-30-13: I definitely have to parse the data into smaller chunks. In one of Apple's example projects, I found a method that does just that. I can't seem to understand exactly how to implement this in my own code, but I am currently attempting to figure out how. Here's the code:

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

我的的ImageView 是黑色的正方形,应该显示我已经送过来的形象。

My imageView is the black square, which should be displaying the image I have sent over.

推荐答案

在iOS 6中,BLE允许你发送数据的20字节块。对于要送你如何切片数据的例子,以及如何使用基于通知传输,下载的BTLE传输苹果应用举例

On iOS 6, BLE allows you to send around 20 byte chunks of data. For an example of how you can slice up the data to be sent and how to use notifications based transmission, download the BTLE Transfer Apple Example application.

要更好地理解什么速度就可以实现的,我建议你分析一下这个图<一href=\"http://www.scriptreactor.com/conn_interval-throughput.pdf\">http://www.scriptreactor.com/conn_interval-throughput.pdf它显示了可实现的速度,因为连接间隔的函数。 iOS不给你这样的细粒度控制,但这个信息仍然是有价值的,为您与计算。

To get a better understanding of what speeds you can achieve, I suggest you analyze this diagram http://www.scriptreactor.com/conn_interval-throughput.pdf It shows the achievable speeds as a function of connection interval. iOS does not give you such a fine grained control but this info is still valuable for you to calculate with.

这篇关于蓝牙4.0 LE发送图片文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆