A moment of relaxation
Http://t.cn/RhfSa04
Recently in doing a Bluetooth related project, need to be in the background of the application, or the phone belongs to the lock screen state, still keep the Bluetooth connection, and can normally receive data.
Originally will be very troublesome, but study the next. Discovery is only 2 steps. The simple can't be simpler.
All right. The following is a concrete approach to implementation.
1. In the Xxx-info.plist file, create a new row Required background modes , add the following two items.
app shares data using Corebluetooth and app communicates using Corebluetooth
As shown in the figure:
After adding this, you will find that Bluetooth remains connected when the application is in the background.
But, after entering the backstage, although the application still hangs, can receive the data normally. However, to the data, if we need to respond in real time, it will use the push.
That is, when the data comes, a pop-up box prompts the user for the data.
2. Set local push
The method here is written in APPDELEGATE.M. Receivedata corresponds to the response function that you receive the data. [CPP] View plain copy-(void) Receivedata: (nsdata*) data { nslog (@ "received data"); //receive data, settings 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 the data"; noti.alertaction = @ "Open"; // Number of noti.applicationiconbadgenumber in the red circle displayed on the icon = 1; //set userinfo convenient to use when needed to be undone later nsdictionary *infodic = [ nsdictionary dictionarywithobject:@ "name" forkey:@ "key"]; noti.userInfo = infoDic; //add push to uiapplication uiapplication *app = [uiapplication sharedapplication]; [app scheduleLocalNotification:noti]; } }
[CPP] View plain copy #pragma mark - received push - (void) application: (uiapplication *) Application didreceivelocalnotification: (uilocalnotification*) notification { uialertview *alert = [[uialertview alloc] initwithtitle:@ "Caller Tip" message:notification.alertbody delegate:nil cancelbuttontitle:@ "Answer" otherbuttontitles:@ "Hang Up",nil]; [alert show]; //Here, you can go through notification useinfo, do something you want to do application.applicationIconBadgeNumber -= 1; }