1.在xxx-info.plist文件中, 新建一行 Required background modes , 加入下面兩項。app
App shares data using CoreBluetooth 和 App communicates using CoreBluetooth
函數
如圖所示:ui
加入這個項後, 你會發現, 當應用進入後臺後, 藍牙仍是保持鏈接的。spa
可是, 進入後臺後, 雖然應用還掛着, 可以正常接收數據。可是, 來數據了, 若是須要咱們實時響應, 那就要用到推送了。.net
也就是, 當數據來的時候, 彈出一個提示框, 提示用戶來數據了。code
2. 設置本地推送orm
這裏的方法寫在AppDelegate.m中。 receiveData對應你接收到數據的響應函數。htm
[cpp] view plaincopyblog
-(void)receiveData:(NSData*)data ip
{
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 plaincopy
#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;
}