1.在xxx-info.plist文件中, 新建一行 Required background modes , 加入下面两项。 App shares data using CoreBluetooth 和 App communicates using CoreBluetooth 如图所示:  加入这个项后, 你会发现, 当应用进入后台后, 蓝牙还是保持连接的。 但是, 进入后台后, 虽然应用还挂着, 能够正常接收数据。但是, 来数据了, 如果需要我们实时响应, 那就要用到推送了。 也就是, 当数据来的时候, 弹出一个提示框, 提示用户来数据了。 2. 设置本地推送 这里的方法写在AppDelegate.m中。 receiveData对应你接收到数据的响应函数。
-(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 = @"打开";
-
- noti.applicationIconBadgeNumber = 1;
-
- NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- noti.userInfo = infoDic;
-
- UIApplication *app = [UIApplication sharedApplication];
- [app scheduleLocalNotification:noti];
- }
- }
|