ios - 当用户关闭和打开蓝牙时,多点连接停止工作

标签 ios ios7 bluetooth-lowenergy multipeer-connectivity

我在我的应用程序中使用 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 应用委托(delegate)方法中重新初始化所有与 Multipeer Connectivity Framework 相关的实例。当您的应用程序在控制中心打开然后关闭后再次激活时,会调用该方法。

关于ios - 当用户关闭和打开蓝牙时,多点连接停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19066105/

相关文章:

ios - 在 iOS 10 中使用远程通知(富媒体推送通知)在通知栏中显示图像

ios - 如何使用非可选属性子类化 UITableViewController

安全服务器上的 iOS 7 企业分发

android - 在 Android 中检测蓝牙 Le 设备

android - 了解 Android BLE ScanSettings 参数

ios - 使用适用于 iOS 的 Google Places API 声明类型时出错

ios - dequeueReusableCellWithIdentifier 返回 nil

ios - UIImagePickerController 仅在 iOS 7 - iPad 上崩溃

ios - 在 iOS 8 中推送/弹出 View 时显示/隐藏导航栏

android - 连接BLE设备后如何获取电池电量?