【问题标题】:Multipeer Connectivity stops working when user turns off and on bluetooth当用户关闭和打开蓝牙时,Multipeer Connectivity 停止工作
【发布时间】:2013-09-28 10:51:09
【问题描述】:

我在我的应用程序的 ios7 中使用多点连接。文件发送和接收工作绝对正常,但是当用户从我的应用程序中访问控制中心(甚至设置)并关闭蓝牙或 wifi 时,文件交换停止工作。当用户将他们两个都重新打开时,它仍然不起作用。为了让它们再次工作,用户必须关闭并重新打开应用程序。

文件以这种方式发送:

MCSession *session = [[MCSession alloc]
                                          initWithPeer:key];


                    NSCalendar *gregorian = [[NSCalendar alloc]
                                             initWithCalendarIdentifier:NSGregorianCalendar];

                    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
                    dateComponents.year = 2100;
                    dateComponents.month = 1;
                    dateComponents.day = 1;
                    dateComponents.hour = 0;
                    dateComponents.minute = 0;
                    dateComponents.second = 0;

                    NSDate *referenceDate = [gregorian dateFromComponents: dateComponents];

                    NSDate *now = [NSDate date];
                    NSTimeInterval interval = [now timeIntervalSinceDate:referenceDate];

                    NSData *Recording = [NSData dataWithContentsOfFile:myFilePath];


                    NSString* str = [NSString stringWithFormat:@"%@.ext", button.titleLabel.text];


                    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                    [dict setObject:str forKey:@"fileName"];
                    [dict setObject:@"Recording" forKey:@"fileType"];
                    [dict setObject:Recording forKey:@"FileData"];

                    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dict];

                    [browser invitePeer:key
                              toSession:session
                            withContext:myData
                                timeout:interval];

用户可以随时使用以下方式重新加载设备:

[browser startBrowsingForPeers];

我认为问题在于超时,但我不确定。

【问题讨论】:

  • 您需要在- (void)applicationDidBecomeActive:(UIApplication *)application 应用委托方法中重新初始化会话。当您的应用在控制中心打开然后关闭后再次变为活动状态时,将调用该方法。
  • 重新初始化会话是指 MCSession 甚至是 MCNearbyServiceAdvertiser 和 MCNearbyServiceBrowser?
  • 如果可以,请重新初始化与 Multipeer Connectivity Framework 相关的所有内容。您还需要重新连接所有已连接的对等点。
  • 好的,做到了,问题似乎解决了,但由于某种原因,不仅所有设备都重新出现,而且设备本身也重新出现。设备似乎可以向自己发送文件...
  • 在不同的框架下也发生过这种情况。您可以尝试将对等点识别为自身,如果您列出对等点,则不显示它。

标签: ios ios7 bluetooth-lowenergy multipeer-connectivity


【解决方案1】:

您需要在- (void)applicationDidBecomeActive:(UIApplication *)application 应用委托方法中重新初始化所有与Multipeer Connectivity Framework 相关的实例。当您的应用在控制中心打开然后关闭后再次变为活动状态时,将调用该方法。

【讨论】:

    【解决方案2】:

    我建议您改为监视连接状态的更改,并在您检测到更改时拆除或重新初始化。这是我使用出色的 Reachability 套件解决同样问题的方法:

    - (void)monitorReachability
    {
        // Allocate a reachability object
        self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
    
        [[NSNotificationCenter defaultCenter] addObserverForName:kReachabilityChangedNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
            Reachability *reachability = note.object;
            if ( reachability.isReachable ) {
                self.isPhysicallyConnected = YES;
                [self updateNearbyService];
            } else {
                self.isPhysicallyConnected = NO;
                [self tearDownNearbyService];
            }
        }];
    
        // Start the notifier, which will cause the reachability object to retain itself!
        [self.reachability startNotifier];
    }
    

    teardownupdate 中,您将重建您的Multipeer 堆栈。

    【讨论】:

    • 有趣。但是如果谷歌出现故障,或者互联网连接中断怎么办?然后程序将无缘无故地重新启动会话。 OP是在谈论蓝牙,而不是wifi。 (仅供参考,我不是反对者)
    • 感谢您的意见。是的,没有专门检测蓝牙连接状态的方法(我知道从 iOS 7 开始)。这就是目前对原始问题实际上没有完美答案的原因,因为“每次应用程序出现时都重新启动会话”也是次优的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2011-08-09
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2017-05-23
    相关资源
    最近更新 更多