iOS development-Core Bluetooth Bluetooth development

Source: Internet
Author: User

Core Bluetooth

Core bluetooth test is troublesome, under normal circumstances, must have at least 2 real Bluetooth 4.0 devices

How can I get the iOS simulator to test the Bluetooth 4.0 program?
Buy a CSR Bluetooth 4.0 USB adapter, plugged in on Mac
In terminal input sudo nvram bluetoothhostcontrollerswitchbehavior= "never"
Restart your Mac
Debug your code with Xcode 4.6 and run the program on an iOS 6.1 emulator
(Apple removed the support for BLE from the iOS 7.0 simulator)

Core Bluetooth Usage Scenarios
Operation Ring, smart home, embedded equipment and so on (financial card reader, ECG measuring device)

Core Bluetooth cores structure diagram

Basic knowledge of Core Bluetooth

Each Bluetooth 4.0 device presents its own service and feature (characteristic)
A device must contain one or more services, each of which contains several features

feature is the smallest unit of interaction with the outside world
For example, a Bluetooth 4.0 device, with feature a to describe their factory information, with feature B to send and receive data

Services and features are uniquely identified with UUID, which distinguishes different services and features with UUID

The functions of each service and feature (characteristic) in the device are provided by the hardware manufacturers of Bluetooth devices, such as which are used for interactive (read/write), which can get the module information (read-only), etc.

Development steps for Core bluetooth

Establishment of central equipment
Scanning peripherals (Discover peripheral)
Connecting peripherals (Connect peripheral)
Scanning services and features in peripherals (Discover services and characteristics)
Data interaction using features and peripherals (Explore and Interact)
Disconnecting (Disconnect)

Current status of Bluetooth

Most smartphones support Bluetooth 4.0 (BLE)

Bluetooth chips are developing quickly and are improving in both performance and efficiency, and are becoming smaller and cheaper.

IBeacon + bluetooth, bright future
One application: Indoor navigation
Estimote Company provides base station for ibeacon
3 ibeacon base stations with a pre-order price of USD 99 (approximately RMB 610)
Estimote company launched the Ibeacon base station of the farthest transmission distance of 50m, but they recommend the use of the 10m range of the best results

A coin cell battery can provide up to 2 years of service life for a ibeacon base station, and it is the case that the device continuously emits signals

Core Bluetooth Development Example
#Import "ViewController.h"#Import<CoreBluetooth/CoreBluetooth.h>@interfaceViewcontroller () <cbcentralmanagerdelegate, cbperipheraldelegate>/** * Peripherals * *@property(Nonatomic, Strong) Nsmutablearray *peripherals;/** * Center Manager * *@property(Nonatomic, Strong) Cbcentralmanager *mgr;@end@implementationviewcontroller-(Nsmutablearray *) peripherals{if(!_peripherals)    {_peripherals = [Nsmutablearray array]; }return_peripherals;} - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib.    //1. Create a central deviceCbcentralmanager *mgr = [[Cbcentralmanager alloc] init]; Self.mgr = Mgr;//Set up proxyMgr.delegate = self;//2. Scan external devices with center equipment    / * If the specified array represents a scan of only the specified device * /[Mgr Scanforperipheralswithservices:nil options:nil];} #pragma mark-cbcentralmanagerdelegate-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: ( Nsdictionary *) Advertisementdata RSSI: (NSNumber *) rssi{//Save scan to external device    //Determine if the array does not contain the current scan to the external settings to save    if(! [Self.peripherals Containsobject:peripheral])        {peripheral.delegate = self;    [Self.peripherals addobject:peripheral]; }}/** * Analog Click, then connect all peripherals * /- (void) start{ for(Cbperipheral *peripheral in self.peripherals) {/** * Connecting peripherals * /[Self.mgr connectperipheral:peripheral Options:nil]; }}/** * Connection peripheral successfully called */- (void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (cbperipheral *) peripheral{//Scan the peripherals to get services[Peripheral discoverservices:nil];}/** * Connecting peripheral failed call * /- (void) Centralmanager: (Cbcentralmanager *) Central diddisconnectperipheral: (cbperipheral *) Peripheral Error: (NSERROR *) error{} #pragma makr-cbperipheraldelegate/** * Once the service is scanned, * * @param The peripheral of the peripheral service */- (void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nserror *) error{//Get all the scanned services in the peripheralsNsarray *services = peripheral.services; for(Cbservice *service in Services) {//Get the services you need        if([Service. Uuid. Uuidstring isequaltostring:@"123"])        {//Find the required features from the services you need            //Scan features from peripheral service[Peripheral Discovercharacteristics:nil Forservice:service]; }    }}/** * As long as the scan to the feature will call * * @param The peripherals to which the peripheral feature belongs * @param service Feature's Services */- (void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: (NSError *) error{//Get all the special functions in the serviceNsarray *characteristics = service.characteristics;//Traverse features to get the required feature handling     for(cbcharacteristic * characteristic in characteristics) {if([Characteristic. Uuid. Uuidstring isequaltostring:@"8888"]) {NSLog (@"Set Alarm"); }    }}@end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS development-Core Bluetooth Bluetooth development

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.