• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:24:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

还附上了它的截图。 Multiple duplicate peer appears

我正在使用下面的代码来显示 GKPeerPickerController。


-(IBAction)btnConnectClickedid)sender
 {
    [self openPeerPickerController];
 }

-(IBAction)btnDisconnectClickedid)sender
{
    [currentSession disconnectFromAllPeers];
}
-(void)openPeerPickerController
{
    if (!currentSession)
    {
        GKPeerPickerController *peerPicker2 = [[GKPeerPickerController alloc] init];
        peerPicker2.delegate = self;
        peerPicker2.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
        [peerPicker2 show];
    }
}

-(void)peerPickerControllerGKPeerPickerController *)picker didConnectPeerNSString *)peerID toSessionGKSession *) session
{
    NSLog(@"eer session connected");
    //set session delegate and dismiss the picker
    session.delegate = self;
    currentSession = session;
picker.delegate = nil;
    [picker dismiss];
}
- (GKSession *)peerPickerControllerGKPeerPickerController *)picker sessionForConnectionTypeGKPeerPickerConnectionType)type
{
    //create ID for session
    NSString *sessionIDString = @"MTBluetoothSessionID";
    //create GKSession object
    GKSession *session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    return session;
}

-(void)peerPickerControllerDidCancelGKPeerPickerController *)picker
{
    NSLog(@"eer cancelled");
    [currentSession disconnectFromAllPeers];
    currentSession=nil;
    picker.delegate = nil;
}
-(void)sessionGKSession *)session peerNSString *)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
            [currentSession setDataReceiveHandler:self withContext:nil]; //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;
    }
}



Best Answer-推荐答案


你的 GKPeerPickerControllerDelegate 方法

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type

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

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.

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

@property (nonatomic, string) GKSession *session;
#define sessionIDString @"MTBluetoothSessionID"

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

并将委托(delegate)方法更改为:

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
    return self.session;
}

确保在完成后取消 session 。

关于ios - GKPeerPicker Controller 中出现重复的对等名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24257080/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap