本文介绍了重新连接断开的同龄人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序了iOS 7 Multipeer框架,但我遇到与设备断开的问题。如果我在两个设备打开应用程序:设备A和设备B两台设备相互连接自动。然而,在从设备B即几秒钟装置的断开起初的连接是这样的:

I'm using the iOS 7 Multipeer framework in my app but I'm experiencing a problem with devices disconnecting. If I open the app in two devices: device A and device B the two devices connect to each other automatically. However, after several seconds device A disconnects from device B. i.e. At first the connection is like this:

A ---> B
A <--- B

几秒钟后:

A ---> B
A      B

设备维护它的连接,但设备B获取一个MCSessionStateNotConnected。

Device A maintains it's connection but device B get's a MCSessionStateNotConnected.

这意味着A发送数据到B,但B不能回答。我试图通过该设备是否连接检查来解决这个问题,如果不是的话,重新启动使用该连接:

This means that A can send data to B but B can't reply. I tried to get around this by checking if the device is connected and if it's not, re-initiating the connection using:

[browser invitePeer:peerID toSession:_session withContext:Nil timeout:10];

但didChangeState回调先手与MCSessionStateNotConnected调用。

But the didChangeState callback just get's called with MCSessionStateNotConnected.

奇怪的是,如果我的应用程序发送A到后台,然后重新打开它,B重新连接到它,并保持连接。

Strangely if I send app A to the background, then re-open it, B reconnects to it and the connection is maintained.

该Multipeer API(和文档)似乎有点稀疏,所以我假定这将只是工作。在这种情况下,我应该如何在设备重新连接?

The Multipeer API (and documentation) seems a bit sparse so I was assuming that it would just work. In this situation how should I re-connect the device?

推荐答案

我有同样的问题,它似乎在同一时间已涉及到我的应用程序浏览和广告,并发送/接受两份邀请。当我停止这样做,让一个对等推迟到其他的邀请函设备仍保持连接状态。

I was having the same problem, and it seems to have been related to my app browsing and advertising at the same time, and two invitations being sent/accepted. When I stopped doing this and let one peer defer to the other for invitations the devices stayed connected.

在我的浏览器委托我检查的散列值的发现同行的显示名,只发送邀请函,如果我的同行中有较高的哈希值:

In my browser delegate I'm checking the hash value of the discovered peer's displayName and only sending an invitation if my peer has a higher hash value:

修改

正如@Masa指出的价值的NSString 将在32位和64位器件不同,所以它的安全使用比较方法上的显示名

As pointed out by @Masa the hash value of an NSString will be different on 32 and 64 bit devices, so it's safer to use the compare: method on displayName.

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {

    NSLog(@"Browser found peer ID %@",peerID.displayName);

    //displayName is created with [[NSUUID UUID] UUIDString]

    BOOL shouldInvite = ([_myPeerID.displayName compare:peerID.displayName]==NSOrderedDescending);

    if (shouldInvite)
        [browser invitePeer:peerID toSession:_session withContext:nil timeout:1.0];
    else
        NSLog(@"Not inviting");
}

正如你所说,该文件是稀疏如此谁知道苹果真的要我们做,但我已经对发送和使用一个会话接受邀请,同时也创造了接受邀请每一个新的会话试验/发,但做事的这种特别的方式给了我最大的成功。

As you say, the documentation is sparse so who knows what Apple really wants us to do, but I've experimented with both sending and accepting invitations using a single session, and also creating a new session for each invitation accepted/sent, but this particular way of doing things has given me the most success.

这篇关于重新连接断开的同龄人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:12