L'envoi de fichier image via Bluetooth 4.0 LE

Je suis en train d'envoyer un .fichier image png à partir d'un Appareil iOS à un autre via Bluetooth 4.0 LE.

Je suis capable de simples morceaux de données comme les cordes, mais ne parvient pas à envoyer et à utiliser les fichiers d'image.

Dans le périphérique, j'ai commencer avec ce

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

Puis-je faire myData une caractéristique de la valeur.

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

Dans le centre de l'Appareil, j'ai ceci quand la caractéristique de la valeur est mise à jour

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

MODIFIER 8-27-13: je suis en mesure d'envoyer plus d'une seule couleur 1by1 pixel .fichier png avec succès que le fichier est d'environ 5 600 octets. Je n'arrive pas à envoyer un multi-couleur 20by20pixel .fichier png qui est seulement d'environ 2 000 octets. Je suis en mesure d'envoyer plus de la taille de fichier qui contient une seule couleur et moins de pixels, mais impossible d'envoyer le fichier plus petit qui contient beaucoup de couleurs et plus de pixels.

MODIFIER 8-30-13:, je dois analyser les données en petits morceaux. Dans une des Apple des exemples de projets, j'ai trouvé une méthode qui ne fait que cela. Je n'arrive pas à comprendre exactement comment implémenter cela dans mon propre code, mais je suis actuellement en train de tenter de comprendre comment. Voici le 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;
}
}}

Mon imageView est le carré noir, ce qui devrait être l'affichage de l'image que j'ai envoyé plus de.
L'envoi de fichier image via Bluetooth 4.0 LE

Avez-vous en mesure d'envoyer l'image à l'aide de cette?

OriginalL'auteur tagabek | 2013-08-27