iOS开发- 蓝牙后台接收数据(BLE4.0) .

来源:互联网 发布:安卓软件效果器 编辑:程序博客网 时间:2024/05/22 03:23
iOS开发- 蓝牙后台接收数据(BLE4.0) .
2014-08-10 16:34:26 来源: 作者: 【大 中 小】 浏览:1次 评论:0条 

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

App shares data using CoreBluetooth  和  App communicates using CoreBluetooth

如图所示:

 

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

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

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

 

2. 设置本地推送

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

 

-(void)receiveData:(NSData*)data  
  1. {  
  2.     NSLog(@"收到数据了");  
  3.       
  4.     //收到数据, 设置推送   
  5.     UILocalNotification *noti = [[UILocalNotification alloc] init];  
  6.     if (noti)  
  7.     {  
  8.         //设置时区   
  9.         noti.timeZone = [NSTimeZone defaultTimeZone];  
  10.         //设置重复间隔   
  11.         noti.repeatInterval = NSWeekCalendarUnit;  
  12.         //推送声音   
  13.         noti.soundName = UILocalNotificationDefaultSoundName;  
  14.         //内容   
  15.         noti.alertBody = @"接收到数据了";  
  16.         noti.alertAction = @"打开";  
  17.         //显示在icon上的红色圈中的数子   
  18.         noti.applicationIconBadgeNumber = 1;  
  19.         //设置userinfo 方便在之后需要撤销的时候使用   
  20.         NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];  
  21.         noti.userInfo = infoDic;  
  22.         //添加推送到uiapplication   
  23.         UIApplication *app = [UIApplication sharedApplication];  
  24.         [app scheduleLocalNotification:noti];  
  25.     }  
  26. }  
0 0
原创粉丝点击