菜鸟教程小白 发表于 2022-12-12 10:24:42

ios - GKPeerPicker Controller 中出现重复的对等名称


                                            <p><p>如果我在两个 ios 设备上多次连接和断开蓝牙,我会得到重复的 peername。</p>

<p>有没有办法在 gkpeerpickercontroller 中为蓝牙聊天应用程序获取唯一对等点的单个名称。</p>

<p>还附上了它的截图。
<img src="/image/efFX0.png" alt="Multiple duplicate peer appears"/> </p>

<p>我正在使用下面的代码来显示 GKPeerPickerController。</p>

<hr/>

<pre><code>-(IBAction)btnConnectClicked:(id)sender
{
    ;
}

-(IBAction)btnDisconnectClicked:(id)sender
{
    ;
}
-(void)openPeerPickerController
{
    if (!currentSession)
    {
      GKPeerPickerController *peerPicker2 = [ init];
      peerPicker2.delegate = self;
      peerPicker2.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
      ;
    }
}

-(void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *) session
{
    NSLog(@&#34;Peer session connected&#34;);
    //set session delegate and dismiss the picker
    session.delegate = self;
    currentSession = session;
picker.delegate = nil;
    ;
}
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
    //create ID for session
    NSString *sessionIDString = @&#34;MTBluetoothSessionID&#34;;
    //create GKSession object
    GKSession *session = [ initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    return session;
}

-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
    NSLog(@&#34;Peer cancelled&#34;);
    ;
    currentSession=nil;
    picker.delegate = nil;
}
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    switch (state)
    {
      case GKPeerStateAvailable:
      {
         // not connected to session, but available for connectToPeer:withTimeout:
      }
      break;

      case GKPeerStateUnavailable:
      {
            // no longer available
      }
            break;

      case GKPeerStateConnected:
      {
            // connected to the session
            ; //set ViewController to receive data
      }
            break;

      case GKPeerStateDisconnected:
      {
            // disconnected from the session
            currentSession.delegate = nil;
            currentSession = nil; //allow session to reconnect if it gets disconnected
      }
            break;

      case GKPeerStateConnecting:
      {
            // waiting for accept, or deny response
      }
            break;

      default:
            break;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你的 GKPeerPickerControllerDelegate 方法</p>

<pre><code>- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
</code></pre>

<p>每次都返回一个新 session 。在您的情况下,它被调用两次,因此创建了两个 session 。
来自文档:</p>

<blockquote>
<p>When the peer picker needs a session, it calls this method. Your
application can either create a new session or return a previously
created session to the peer picker.</p>
</blockquote>

<p>因此您可以将 session 声明为一个属性并编写一个 getter,然后在委托(delegate)中返回 session 属性,这将避免创建多个 session </p>

<pre><code>@property (nonatomic, string) GKSession *session;
#define sessionIDString @&#34;MTBluetoothSessionID&#34;

- (GKSession) session {
    if(!_session) {
      //create GKSession object
      _session = [ initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    }
    return _session;
}
</code></pre>

<p>并将委托(delegate)方法更改为:</p>

<pre><code>- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
    return self.session;
}
</code></pre>

<p>确保在完成后取消 session 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - GKPeerPickerController 中出现重复的对等名称,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/24257080/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/24257080/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - GKPeerPicker Controller 中出现重复的对等名称