1

What I want to do is exchange data between plists via bluetooth from one iPad to another. Short summary of my situation, I have dictionary that each plist fills from each iPad and it goes kind of like this:

iPad1 would have: MAINdictionary(dictionary1(dictionaryA, dictionaryB, dictionaryC), dictionary2(dictionaryD, dictionaryE, dictionaryF))

iPad2 would have: MAINdictionary(dictionary1(dictionaryG, dictionaryH, dictionaryI), dictionary2(dictionaryJ, dictionaryK, dictionaryL))

The parenthesis indicate that the dictionary contains the items within the parenthesis. I would like my final result to have both iPads being clones of each other and contain an updated list with all data like this:

MAINdictionary(dictionary1(dictionaryA, dictionaryB, dictionaryC, dictionaryG, dictionaryH, dictionaryI), dictionary2(dictionaryD, dictionaryE, dictionaryF, dictionaryJ, dictionaryK, dictionaryL))

I could most likely code the solution, I'm just having difficulty coming up with a strategy. I am fairly new to Core Data, so go a little easy on me if you can please.

Louie Bertoncin
  • 3,744
  • 2
  • 25
  • 28

2 Answers2

1

Check out the BTLE Transfer sample code from Apple. It only does sharing in one direction, but you can easily extend it to do both directions if needed.

Also, I don't think you need core data if you just want to merge the dictionaries over Bluetooth!

0

Using Core Bluetooth:

On peripheral side within the appropriate method (i.e. peripheralManager:didReceiveReadRequest:)

NSError *error = nil;
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:anObject forKey:@"key"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
    //respond to error appropriately
}
request.value = jsonData;
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];

On Central side within the appropriate method (i.e. peripheral:didUpdateValueForCharacteristic:error:)

NSError *error = nil;
NSData *jsonDataFromPeripheral = characteristic.value;
NSDictionary *theDictionary = [NSJSONSerialization JSONObjectWithData: jsonDataFromPeripheral options: NSJSONReadingAllowFragments error:&error];
if ([theDictionary objectForKey:@"key"]) {
    NSLog(@"Success!");
}

If you would like to send data to a peripheral then you can write to a discovered characteristic like this:

NSError *error = nil;
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:anObject forKey:@"key"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
[peripheral writeValue:jsonData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

Make sure the peripheral's characteristic is setup so that reads and writes may be performed. You do this when creating a CBMutableCharacteristic. Here is an example:

CBMutableCharacteristic *characterisitic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:CHARACTERISTIC_STRING] properties:CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];

Notice that you do not have to create separate characteristics for a peripheral's service if you would like your central to implement both reads and writes.

Justin Moser
  • 2,005
  • 3
  • 22
  • 28