2013-03-13 65 views
1

我想要做的是通过蓝牙将plists间的数据从一个iPad交换到另一个。我的情况的简短摘要,我有字典,每个plist从每个iPad填充,它有点像这样:通过蓝牙在NSDictionaries之间交换数据

iPad1应该有:MAINdictionary(dictionary1(dictionaryA,dictionaryB,dictionaryC),dictionary2(dictionaryD,dictionaryE,dictionaryF ))

的iPad2将有:MAINdictionary(dictionary1(dictionaryG,dictionaryH,dictionaryI),dictionary2(dictionaryJ,dictionaryK,dictionaryL))

括号表明该词典包含括号内的项目。我想我的最终结果是两个iPad都是相互克隆的,并包含一个更新列表,其中包含所有这类数据:

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

我很可能编码解决方案,我只是很难提出一个策略。我对Core Data相当陌生,所以如果可以的话,请稍微轻松一点。

回答

1

查看来自Apple的BTLE Transfer示例代码。它只能在一个方向上共享,但如果需要,您可以轻松地将其扩展为双向。

另外,我不认为你需要核心数据,如果你只是想通过蓝牙合并字典!

0

使用核心蓝牙:

在周侧的适当的方法中(即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]; 

在适当的方法中中央侧(即周:didUpdateValueForCharacteristic:错误:)

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

如果你想发送数据到periph全部擦除,那么你可以写这样的发现特征:

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]; 

确保外围的特点是设置,使读取和写入可以进行。您在创建CBMutableCharacteristic时执行此操作。这里有一个例子:

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

请注意,您不必创建一个外设的服务特点分开,如果你想你的中心来实现读取和写入。