的GameKit蓝牙传输问题 [英] GameKit Bluetooth Transfer Problem

查看:116
本文介绍了的GameKit蓝牙传输问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的GameKit框架,通过蓝牙发送文件。我有但问题是,我只能在同一时间发送一个NSData对象,但我需要将其保存在另一端。这显然​​是不可能不知道文件名,但我不知道如何传送的。我试着将它转换为字符串的NSData *数据= [NSData的dataWithContentsOfFile:urlAddress]; 但我只能发送一个NSData对象,而不是两个。

I am trying to send a file via Bluetooth using the GameKit framework. The problem I am having though is that I can only send one NSData object at a time, but I need to save it on the other end. this obviously isn't possible without knowing the filename, but i don't know how to transmit that. I've tried to convert it to a string NSData*data = [NSData dataWithContentsOfFile:urlAddress]; but i can only send one NSData object, not two.

有没有人碰到这个问题了吗?

Has anyone come across this problem yet?

推荐答案

与的GameKit工作了一段时间,我发现,有大约每90K发送所以,如果你是文件中的限制更大然后90K你必须打破它。这里是我会建议你打破东西:

Working with the GameKit for a while I've found that there is a limit of about 90k per 'send' so if you're file is larger then 90k you'll have to break it up. Here is how I would suggest you break things up:

1 - 发送的文件的名称

1st - Send the name of your file

NSData* fileNameData = [fileNameStr dataUsingEncoding: NSASCIIStringEncoding];
// send 'fileNameData'

2日 - 发送块的数目您要发送

2nd - Send the number of chunks your going to send

NSUInteger fiftyK = 51200;
NSUInteger chunkCount = (((NSUInteger)(srcData.length / fiftyK)) + ((srcData.length % fiftyK) == 0 ) ? 0 : 1))
NSString chunkCountStr = [NSString stringWithFormat:@"%d",chunkCount];
NSData* chunkCountData = [chunkCountStr dataUsingEncoding: NSASCIIStringEncoding];
// send 'chunkCountData'

3日 - 分手并发送NSData对象为一组每个小于50K NSObjects的(只是为了安全大小)

3rd - Break up and send your NSData object into a set of NSObjects of less then 50k each (just to be on the safe size)

NSData *dataToSend;
NSRange range = {0, 0};
for(NSUInteger i=0;i<srcData.length;i+=fiftyK){
  range = {i,fiftyK};
  dataToSend = [srcData subdataWithRange:range];
  //send 'dataToSend'  
}
NSUInteger remainder = (srcData.length % fiftyK);
if (remainder != 0){
  range = {srcData.length - remainder,remainder};
  dataToSend = [srcData subdataWithRange:range];
  //send 'dataToSend'  
}

在接收端你要做到以下几点:

On the receiving side you'll want to do the following:

1 - 接收的文件名

1st - Receive the file name

// Receive data
NSString* fileNameStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]

2日 - 接收块的数量,您即将接受

2nd - Receive the number of chunks you are about to receive

// Receive data
NSString* chunkCountStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]
NSUInteger chunkCount = [chunkCount intValue];

3 - 接收的数据块

3rd - Receive the chunks of data

NSMutableData data = [[NSMutableData alloc]init];
for (NSUInteger i=0; i<chunkCount;i++){
  // Receive data
  [data appendData:receivedData];
}

如果一切都已经工作的权利,你现在拥有一个包含文件名和数据的 fileNameStr 对象包含对象您的文件的内容。

If everything has worked right you will now have a fileNameStr object containing your file name and a data object containing the contents of your file.

希望这有助于 - AYAL

Hope this helps - AYAL

这篇关于的GameKit蓝牙传输问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆