iOS development-Bluetooth background receive data (BLE4.0)

Source: Internet
Author: User
Tags set time

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;  }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.