简体   繁体   English

确定GKSession蓝牙设备何时超出范围

[英]Determining when GKSession bluetooth devices go out of range

I'm using GKSession, GKSessionDelegate to implement peer to peer bluetooth connectivity between a number of iX (iPod, iPad, iPhone, ...) devices. 我正在使用GKSession, GKSessionDelegate在许多iX(iPod,iPad,iPhone等)设备之间实现对等蓝牙连接。 I want to display a list of the currently available/connected devices in range . 我想显示range中当前可用/已连接设备的列表。

I'm currently relying on 我目前依靠

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state

to do this. 去做这个。 Unfortunately, this method does not appear to be reliably called when a device goes out of range. 不幸的是,当设备超出范围时,似乎无法可靠地调用此方法。 Is there a "better" way to determine if a device is in range? 有没有“更好”的方法来确定设备是否在范围内?

Some code: 一些代码:

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {

    NSString* connectionStateString=
    (state==GKPeerStateAvailable)?@"available":
    (state==GKPeerStateUnavailable)?@"unavailable":
    (state==GKPeerStateConnected)?@"connected":
    (state==GKPeerStateDisconnected)?@"disconnected":@"connecting";
    // Add the peer to the Dictionary
    NSArray* details=[NSArray arrayWithObjects:[session displayNameForPeer:peerID],connectionStateString,nil];
    [connectionPeers setObject:details forKey:peerID];

    if (state == GKPeerStateAvailable) {
        NSLog(@"Adding peerID:%@",peerID);
        [session connectToPeer:peerID withTimeout:60];//'connect' to everything, so data can be sent
    }
    else if (state == GKPeerStateUnavailable || state==GKPeerStateDisconnected) {
        [connectionPeers removeObjectForKey:peerID];
    }
    [self listPlayers];
}

GKSession is built on Bonjour over Bluetooth and I believe that your problem is that the bonjour service is still showing as active since Bonjour doesn't invalidate service advertisements when a peer has disconnected from the network. GKSession基于Bonjour通过蓝牙构建,我相信您的问题是Bonjour服务仍然显示为活动状态,因为当对等方与网络断开连接时Bonjour不会使服务广告无效。 I think the mDNS records only expire either when the mDNS cache timeout occurs ( not something you can tweak ) or when the advertising peer manually invalidates the service. 我认为mDNS记录仅在发生mDNS缓存超时(不是您可以调整的时间)时或在广告对等方手动使服务无效时才过期。

I don't think GKSession is going to easily do what you want here via advertisement. 我认为GKSession不会通过广告轻松地完成您想要的操作。 Connected peers should however disconnect once they're out of Bluetooth range. 但是,已连接的对等设备一旦超出蓝牙范围,应断开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM