gpt4 book ai didi

ios - 从多对等连接中禁用 WiFi

转载 作者:行者123 更新时间:2023-12-01 16:03:48 24 4
gpt4 key购买 nike

我已经阅读了文档,但没有太多关于多点连接的信息,这些信息与选择一种可能的对等点连接的媒介有关。

Multipeer Connectivity 基于 WiFi 或蓝牙自动发现对等点。有没有办法将其限制为仅限蓝牙?

最佳答案

正如@kdogisthebest 正确指出的那样,没有办法强制 Multipeer Connectivity 使用特定的网络技术,但是由于您的问题与 WiFi 的特定问题有关,所以这个答案详细说明了我正在做的事情来解决这个问题。

我通过在 discoveryInfo 中发送缩短的时间戳来解决 WiFi 上的“幻像”对等点问题。创建 MCNearybyServiceAdvertiser 时.这里有几个警告:

1)此解决方案假设两个设备具有相同的时间。我通过使用 ios-ntp 的修改版本来确保这一点作为应用程序的时间源。

2) 它还假设广告和浏览不会运行太久。我为发现阶段设置了 60 秒的长度,并且我在每次重新启动时完全重新启动浏览器/广告商。

3) MPC 似乎不喜欢discoveryInfo 中的太多字节。所以基于 epoch 发送一个 NSTimeInterval 是行不通的。我不得不截断它们。

因此,当我的应用程序进入发现模式时,它会同时开始浏览和转换广告。广告代码如下所示:

- (void)startAdvertising {

if (_advertising){
NSLog(@"Already advertising");
return;
}

self.acceptedPeerIDNameMap = [NSMutableDictionary dictionary];

NSInteger timeStamp = [self shortenedNetworkTimeStamp];
NSDictionary *discoveryInfo = @{kAdvertisingDiscoveryInfoTimestampKey:[NSString stringWithFormat:@"%ld",(long)timeStamp]};

NSLog(@"Starting advertiser");

self.serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_myPeerID
discoveryInfo:discoveryInfo
serviceType:kServiceType];
_serviceAdvertiser.delegate = self;

[_serviceAdvertiser startAdvertisingPeer];

self.advertising = YES;
}

方法 shortenedNetworkTimestamp只需要一个 NSTimeInterval(使用 ntp 框架或 timeIntervalSinceReferenceDate 并从中删除 1400000000

然后,当浏览器发现对等点时,它会检查广告商的时间戳是否在已知的发现持续时间内(在我的情况下为 60 秒):
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {

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

//Only one peer should invite the other
BOOL shouldInvite = [peerID.displayName compare:_myPeerID.displayName]==NSOrderedAscending;

//Don't re-send invitations
if (_peerInfoDisplayNameMap[peerID.displayName]){
DLog(@"Already connected to peerID %@",peerID.displayName);
shouldInvite = NO;
}
else if (_invitedPeerIDNameMap[peerID.displayName]){
DLog(@"Already invited peerID %@",peerID.displayName);
shouldInvite = NO;
}

//Invite if discovery info is valid
if (shouldInvite && [self discoveryInfoIsValid:info]) {

DLog(@"Inviting");

_invitedPeerIDNameMap[peerID.displayName] = peerID;

MCSession *session = [self availableSession];
[_serviceBrowser invitePeer:peerID toSession:session withContext:nil timeout:0];
}
else {
DLog(@"Not inviting");
}
}

发现信息有效性检查非常简单 - 只需确保信息中发送的时间戳在发现时间范围内(在我的情况下 kDiscoveryPhaseDuration 为 60 秒):
- (BOOL)discoveryInfoIsValid:(NSDictionary *)info {

BOOL isValid = YES;

NSString *infoTimeStamp = info[kAdvertisingDiscoveryInfoTimestampKey];
NSTimeInterval sentTimeStamp = (infoTimeStamp) ? [infoTimeStamp doubleValue] : -1;
NSTimeInterval currentTimeStamp = [self shortenedNetworkTimeStamp];

if (sentTimeStamp==-1 || (currentTimeStamp - sentTimeStamp) > kDiscoveryPhaseDuration){
DLog(@"Expired discovery info (current=%f, sent=%f)",currentTimeStamp,sentTimeStamp);
isValid = NO;
}

return isValid;
}

希望这会有所帮助。我在自己的代码中处理 MPC 中的许多其他怪癖,但我认为以上内容涵盖了这个特定问题。

关于ios - 从多对等连接中禁用 WiFi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26289811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com