IOS development-receive data in the Bluetooth background (BLE4.0)

Source: Internet
Author: User

Recently, a Bluetooth-related project needs to keep the Bluetooth connection and receive data normally when the application enters the background or the mobile phone is locked.

It will be very troublesome in the future, but after learning it, we can only find two steps. It's easy.


Okay. The following are the specific implementation methods.


1. In the xxx-info.plist file, create a new rowRequired background modes, Add the following two items.

App shares data using CoreBluetoothAndApp communicates using CoreBluetooth

:



After this item is added, you will find that after the application enters the background, the Bluetooth remains connected.

However, after entering the background, even though the application is still hanging, data can be normally received. However, if we need real-time response, we need to use push.

That is, when the data comes, a prompt box is displayed, prompting the user to access the data.


2. Set local push

The method is written in AppDelegate. m. ReceiveData corresponds to the response function of the received data.

-(Void) receiveData :( NSData *) data {NSLog (@ "received data"); // when receiving data, set push UILocalNotification * noti = [[UILocalNotification alloc] init]; if (noti) {// set the time zone noti. timeZone = [NSTimeZone defaultTimeZone]; // you can specify the repetition interval (noti. repeatInterval = NSWeekCalendarUnit; // push noti. soundName = UILocalNotificationDefaultSoundName; // content noti. alertBody = @ "received data"; noti. alertAction = @ "open"; // Number of noti in the red circle on the icon. applicationIconBadgeNumber = 1; // set userinfo to enable NSDictionary * infoDic = [NSDictionary dictionaryWithObject: @ "name" forKey: @ "key"]; noti. userInfo = infoDic; // Add push to uiapplication UIApplication * app = [UIApplication sharedApplication]; [app scheduleLocalNotification: noti];}

# Pragma mark-receive push-(void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Incoming call prompt" message: notification. alertBody delegate: nil cancelButtonTitle: @ "" otherButtonTitles: @ "", nil]; [alert show]; // you can use useinfo of notification, you have done 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.