Tuesday, 27 August 2013

Sending image file over Bluetooth 4.0 LE

Sending image file over Bluetooth 4.0 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.
In the Peripheral device, I start out with this
pictureBeforeData = [UIImage imageNamed:@"myImage.png"];
NSData *myData = UIImagePNGRepresentation(pictureBeforeData);
Then I make myData a characteristic's value.
_myCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:_myCharacteristicUUID
properties:CBCharacteristicPropertyRead
value:myData
permissions:CBAttributePermissionsReadable];
In the Central Device, I have this when the characteristic's value is updated
- (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 }
When I run this, I get this error:
<Error>: ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT
chunk
I looked through this thread and tried the solutions, but nothing seemed
to work. Invalid PNG Image file: iDOT doesn't point to valid IDAT chunk
Right after receiving this error, the size correctly logs out:
SIZE: 160.000000, 160.000000
Does the fact that I can read the size mean that I am successfully sending
the image file over?
When I attempt to add the image to an array, the array always returns a
count of 0.
My imageView is the black square, which should be displaying the image I
have sent over.

No comments:

Post a Comment