13

Bluetooth 4.0 LE を介して、ある iOS デバイスから別の iOS デバイスに .png 画像ファイルを送信しようとしています。

文字列などのデータを単純化することはできますが、画像ファイルを正常に送信して利用することはできません。

周辺機器では、これから始めます

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

次にmyData、特性の値を作成します。

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

EDIT 8-27-13:約 5,600 バイトの単一色 1x1 ピクセルの .png ファイルを正常に送信できました。約 2,000 バイトしかないマルチカラー 20by20pixel .png ファイルを送信できません。1 色のみでピクセル数が少ない大きなファイルは送信できますが、色数が多くピクセル数が多い小さなファイルは送信できません。

編集 8-30-13:データを小さなチャンクに解析する必要があります。Apple のサンプル プロジェクトの 1 つで、まさにそれを行う方法を見つけました。これを自分のコードに実装する方法を正確に理解できないようですが、現在、方法を理解しようとしています。コードは次のとおりです。

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

MyimageViewは黒い四角で、送信した画像が表示されているはずです。 ここに画像の説明を入力

4

2 に答える 2

2

BTLE は、特性と任意のデータ パケットの両方で送信できるデータ量の点で大幅に制限されています。デバイス間の接続を確立してから、画像データを分割し、複数の小さなパケットを送信する必要があります。2013 WWDC ビデオでは、優れた概要とコード サンプルが提供されます。

于 2013-08-27T22:23:44.367 に答える