您的位置:首页 > 其它

蓝牙后台接收数据(BLE4.0)

2016-11-23 10:03 323 查看
原链接:http://blog.csdn.net/hitwhylz/article/details/28664939

1.在xxx-info.plist文件中, 新建一行  Required background modes  , 加入下面两项。

App shares data using CoreBluetooth  和  App communicates using CoreBluetooth

如图所示:



加入这个项后, 你会发现, 当应用进入后台后, 蓝牙还是保持连接的。

但是, 进入后台后, 虽然应用还挂着, 能够正常接收数据。但是,  来数据了, 如果需要我们实时响应, 那就要用到推送了。

也就是, 当数据来的时候, 弹出一个提示框, 提示用户来数据了。

2. 设置本地推送

这里的方法写在AppDelegate.m中。  receiveData对应你接收到数据的响应函数。

[cpp] view
plain copy

 





-(void)receiveData:(NSData*)data  

{  

    NSLog(@"收到数据了");  

      

    //收到数据, 设置推送  

    UILocalNotification *noti = [[UILocalNotification alloc] init];  

    if (noti)  

    {  

        //设置时区  

        noti.timeZone = [NSTimeZone defaultTimeZone];  

        //设置重复间隔  

        noti.repeatInterval = NSWeekCalendarUnit;  

        //推送声音  

        noti.soundName = UILocalNotificationDefaultSoundName;  

        //内容  

        noti.alertBody = @"接收到数据了";  

        noti.alertAction = @"打开";  

        //显示在icon上的红色圈中的数子  

        noti.applicationIconBadgeNumber = 1;  

        //设置userinfo 方便在之后需要撤销的时候使用  

        NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];  

        noti.userInfo = infoDic;  

        //添加推送到uiapplication  

        UIApplication *app = [UIApplication sharedApplication];  

        [app scheduleLocalNotification:noti];  

    }  

}  

[cpp] view
plain copy

 





#pragma mark - 接收到推送  

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification  

{  

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"来电提示"  

                                                    message:notification.alertBody  

                                                   delegate:nil  

                                          cancelButtonTitle:@"接听"  

                                          otherButtonTitles:@"挂断",nil];  

    [alert show];  

    //这里,你就可以通过notification的useinfo,干一些你想做的事情了  

    application.applicationIconBadgeNumber -= 1;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: