1. In the Xxx-info.plist file, create a new row Required background modes , adding the following two items.
app shares data using Corebluetooth and app communicates using Corebluetooth
:
After adding this entry, you will find that Bluetooth remains connected when the app is in the background.
However, after entering the background, although the application is still hanging, can receive data normally. However, to the data, if we need to respond in real time, it will be used to push.
That is, when the data comes, pop up a prompt box, prompting the user to come to the data.
2. Set up local push
The method here is written in APPDELEGATE.M. The receivedata corresponds to the response function that you receive the data.
[CPP] view plaincopy
-(void) Receivedata: (nsdata*) Data
{
NSLog (@"received data");
//Receive data, set push
Uilocalnotification *noti = [[Uilocalnotification alloc] init];
if (Noti)
{
//Set time zone
Noti.timezone = [Nstimezone defaulttimezone];
//Set repeat Interval
Noti.repeatinterval = Nsweekcalendarunit;
//Push sound
Noti.soundname = Uilocalnotificationdefaultsoundname;
//Content
Noti.alertbody = @"received data";
Noti.alertaction = @"open";
//The number of the red circle displayed on the icon
Noti.applicationiconbadgenumber = 1;
//Set UserInfo to be used after subsequent revocation
Nsdictionary *infodic = [nsdictionary dictionarywithobject:@"name" forkey:@"key"];
Noti.userinfo = Infodic;
//Add push to UIApplication
UIApplication *app = [UIApplication sharedapplication];
[App Schedulelocalnotification:noti];
}
}
[CPP] view plaincopy
#pragma mark-received a push
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification*) Notification
{
Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Call Prompt"
Message:notification.alertBody
Delegate:nil
cancelbuttontitle:@"Answer"
otherbuttontitles:@"Hang Off", nil];
[Alert show];
//Here, you can do something you want to do through notification's Useinfo .
Application.applicationiconbadgenumber-= 1;
}
iOS Development-Bluetooth backend receive data (BLE4.0)