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){//电话挂断
        }
    };
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值