发送第一个数据后蓝牙会话立即结束
不明白为什么会发生这种情况,但是当我使用蓝牙向连接的对等方发送数据时,它们很好地接收了数据,但随后立即断开连接,使得进一步的交互变得不可能。谁能给出任何迹象表明为什么会发生这种情况?我尝试将超时更改为 60 秒,尝试将发送的数据量减少到 500 字节以下,但我仍然遇到这种奇怪的情况,即对等点将立即彼此断开连接。是否可能是因为我使用 GKSession 来初始化网络,然后使用 GKPeerPicker 供人们连接到它?
这是我的数据发送和接收代码。这主要是临时的,可能会改变以容纳大量数据,但现在我只想建立一个稳定的连接。
-(void)transmitWholeClusterToPeer:(NSString *)peerID{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[encoder encodeInt:0 forKey:@"Mode"];
[encoder encodeObject:clusterMap.header forKey:@"Header"];
[encoder finishEncoding];
NSArray *peerList = [[[NSArray alloc] initWithObjects:peerID, nil] copy];
[network sendData:data toPeers:peerList withDataMode:GKSendDataReliable error:nil];
[encoder autorelease];
[peerList autorelease];
}
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context{
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
int mode = [decoder decodeIntForKey:@"Mode"];
switch (mode) {
case 0:
clusterMap.header = [decoder decodeObjectForKey:@"Header"];
break;
default:
break;
}
}
Can't see why this is happening, but when I use Bluetooth to send data to connected peers, they receive the data perfectly well but then immediately disconnect making further interaction impossible. Can anyone give any indication as to why this might be happening? I tried changing the timeout to 60 seconds, I tried reducing the amount of data sent to less than 500 bytes, and still I get this strange situation where peers will immediately disconnect from one another. Is it maybe because I'm using a GKSession to initialise the network, then a GKPeerPicker for people to connect to it?
Here's my data sending-and-receiving code. This is mostly temporary and will probably change to accommodate large amounts of data, but right now I just want to establish a stable connection.
-(void)transmitWholeClusterToPeer:(NSString *)peerID{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[encoder encodeInt:0 forKey:@"Mode"];
[encoder encodeObject:clusterMap.header forKey:@"Header"];
[encoder finishEncoding];
NSArray *peerList = [[[NSArray alloc] initWithObjects:peerID, nil] copy];
[network sendData:data toPeers:peerList withDataMode:GKSendDataReliable error:nil];
[encoder autorelease];
[peerList autorelease];
}
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context{
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
int mode = [decoder decodeIntForKey:@"Mode"];
switch (mode) {
case 0:
clusterMap.header = [decoder decodeObjectForKey:@"Header"];
break;
default:
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能肯定地说这是最终的答案,但我已经通过删除 GKPeerPicker 并实现我自己的客户端-服务器设置代码来解决这个问题,该代码本身会生成“保持活动”ping。我认为问题实际上只是我手动设置了一个会话,然后使用对等选择器连接到它。似乎 Peer Picker 只有在连接到其他 Peer Picker 时才稳定且有用。
I can't say for sure this is the definitive answer, but I have solved the problem after a fashion by removing GKPeerPicker and implementing my own Client-Server setup code that generates 'keep alive' pings itself. I think the problem really was only that I was setting up a session manually, then using the Peer Picker to connect to it. It seems that Peer Pickers are only stable and useful when they are connecting to other Peer Pickers.