iOS Multipeer Connectivity广告商和浏览器

时间:2014-07-23 20:21:40

标签: ios objective-c xamarin

我的设置是这样的:我一次运行0到8个设备,可以随时添加或删除设备。

我想使用iOS 7 Multipeer框架来连接它们。我在受控环境中工作,我可以在广告客户模式下启动1-7个设备,然后在浏览器模式下启动它们,它们都会链接起来。

我不确定的是,当我启动它时,我应该如何知道设备是否需要处于广告客户或浏览器模式?我已尝试默认为广告客户模式X秒,然后切换到浏览器,问题在于所有设备可能同时启动并同时关闭广告客户模式。< / p>

我还考虑过在广告客户和浏览器模式下运行设备,但最初的问题是设备会发现自己。另外我相信我用这种方式连接的设备少了一个。

我确定有推荐的方法进行设置,但我一直无法找到任何不会假设设置浏览器和广告客户的内容,任何人都有对此的建议?

1 个答案:

答案 0 :(得分:1)

很容易让所有设备都是广告客户和浏览器,这是一种正常的行为(我很确定在文档中提到这一点,我会搜索它并稍后添加链接)。

我没有遇到设备发现问题的问题,也许你正在为同一设备创建两个peerID?

您可以查看此PLPartyTime's implementation。它只是进行了一些简单的检查,看它是否需要连接/接受连接。

广告商代表

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
       withContext:(NSData *)context
 invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
  // Only accept invitations with IDs lower than the current host
  // If both people accept invitations, then connections are lost
  // However, this should always be the case since we only send invites in one direction
  if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedDescending)
  {
    invitationHandler(YES, self.session);
  }
}

浏览器代理

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
  // Whenever we find a peer, let's just send them an invitation
  // But only send invites one way
  // TODO: What if display names are the same?
  // TODO: Make timeout configurable
  if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedAscending)
  {
    NSLog(@"Sending invite: Self: %@", self.peerID.displayName);
    [browser invitePeer:peerID
              toSession:self.session
            withContext:nil
                timeout:10];
  }
}

您可能还想要check out my fork,它稍微小一些,browseradvertiser都是独立的对象。