ox2A19的专栏

个人技术博客,源于分享

CoreBluetooth后台运行的实现

上一篇中实现了和蓝牙设备的通讯功能,app在前台运行是是没有任何问题的。当然如果你想当你的app处于后台时仍然可以和蓝牙设备通讯时,那就得采用其他的方式来实现了!实现方式如下:

1;在plist文件中进行一下设置


2;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    [self SessionRemote];
    [self CTcallcenter];//电话监听
    [self.window makeKeyAndVisible];
    return YES;
}

3;在使用音乐播放器播放音乐,看电影,接听电话等情况下仍然可以通讯需要在后台协议中进行如下设置 否则只能运行5分钟将会停止

//后台运行事件
- (void)applicationDidEnterBackground:(UIApplication *)application{
    NSLog(@"后台运行事件");
    [[CBperipheral Singleton]starReading];//每隔30s读取一次蓝牙设备的常量值如:电量等
   

    //每隔30s播放一次音频

     readingAudio =[[NSTimer scheduledTimerWithTimeInterval:radingTime
                                                    target:self
                                                  selector:@selector(playVoice)
                                                  userInfo:nil
                                                   repeats:YES] retain];
}
//返回前台
- (void)applicationWillEnterForeground:(UIApplication *)application{
    [[CBperipheral Singleton]stopReading];//取消读取常量值
    if (readingAudio) {//取消播放音频
        [readingAudio invalidate];
    }
}




-(void)SessionRemote{
    
    // 必须正确处理background task,才能在后台发声
    self.bgTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
        self.bgTaskID = UIBackgroundTaskInvalid;
    }];
    
    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setDelegate:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                           error:&sessionError];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

-(void)CTcallcenter{ //电话监听
    CTCallCenter  *center =[[CTCallCenter alloc] init];
    center.callEventHandler=^(CTCall *call){
        if (CTCallStateIncoming == call.callState) {
            remindSet *remind=[[FM_Operating Singleton]select_remind];
            if (remind) {
                if ([remind.phone isEqualToString:@"YES"]) {//电话进来
                    NSLog(@"发送来电提醒指令");
                }
            }
        }else if(CTCallStateDisconnected == call.callState){//电话挂断
        }
    };
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ox2A19/article/details/38900551
文章标签: 蓝牙 plist application
个人分类: BLE Device
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

CoreBluetooth后台运行的实现

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭