IOS development-Core Bluetooth Development
Core Bluetooth
Core Bluetooth testing is troublesome. Normally, at least two real Bluetooth 4.0 devices are required.
How can I enable the iOS simulator to test the Bluetooth 4.0 program?
Buy a CSR Bluetooth 4.0 USB adapter and plug it on Mac
Enter sudo nvram incluthhostcontrollerswitchbehavior = "never" on the terminal"
Restart Mac
Use Xcode 4.6 to debug the code and run the program on the iOS 6.1 simulator.
(Apple removed the support for BLE from the iOS 7.0 simulator)
Use Cases of Core Bluetooth
Sports wristbands, smart home devices, embedded devices, and so on (Financial card readers and ECG Surveyors)
Core Bluetooth Core Structure
Core Bluetooth Basics
Each Bluetooth 4.0 device uses services and Characteristic features to present its own
A device must contain one or more services. Each Service contains several features.
Feature is the smallest unit of external interaction
For example, A Bluetooth 4.0 device uses feature A to describe its factory information and Feature B to send and receive data.
Services and features are uniquely identified by UUID. different services and features can be differentiated by UUID.
Services and characteristic features in a device are provided by hardware vendors of Bluetooth devices. For example, which services are used for interaction (read/write) and which module information can be obtained (read-only) and so on
Core Bluetooth development steps
Create a central device
Scan peripherals (Discover Peripheral)
Connect peripherals (Connect Peripheral)
Scan Services And Characteristics in peripherals)
Using features to Interact with peripherals)
Disconnect)
Current Situation of Bluetooth
Most smartphones support Bluetooth 4.0 (BLE)
Bluetooth chips are growing rapidly, improving performance and efficiency, and making them smaller and cheaper.
IBeacon + Bluetooth, bright future
One application: indoor navigation
Estimote provides base stations for iBeacon
The pre-purchase price for the three iBeacon base stations is USD 99 (about RMB 610)
The maximum transmission distance of the iBeacon base station launched by Estimote is 50 m, but they recommend the best use within 10 m.
A button battery can provide up to two years of service life for an iBeacon base station, and it is used when the device continuously transmits signals to external devices.
Core Bluetooth Development Instance
# Import ViewController. h # import
@ Interface ViewController ()
/*** Peripherals */@ property (nonatomic, strong) NSMutableArray * peripherals;/*** center manager */@ property (nonatomic, strong) CBCentralManager * mgr; @ end @ implementation ViewController-(NSMutableArray *) peripherals {if (! _ Peripherals) {_ peripherals = [NSMutableArray array];} return _ peripherals;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 1. create a central device CBCentralManager * mgr = [[CBCentralManager alloc] init]; self. mgr = mgr; // sets the proxy mgr. delegate = self; // 2. scan external devices with central devices/* If the specified array represents scanning only the specified device */[mgr scanForPeripheralsWithServices: nil options: nil] ;}# Pragma mark-CBCentralManagerDelegate-(void) centralManager :( CBCentralManager *) central Region :( CBPeripheral *) peripheral advertisementData :( NSDictionary *) advertisementData :( NSNumber *) ECC {// Save the scanned external device // you can determine if the array does not contain the currently scanned external settings to save if (! [Self. peripherals containsObject: peripheral]) {peripheral. delegate = self; [self. peripherals addObject: peripheral] ;}}/*** simulate a click and connect all peripherals */-(void) start {for (CBPeripheral * peripheral in self. peripherals) {/*** connect to peripherals */[self. mgr connectPeripheral: peripheral options: nil];}/*** successful call to connect to peripherals */-(void) centralManager :( CBCentralManager *) central didConnectPeripheral :( CBPeripheral *) peripheral {// scan the services in the peripherals [peripheral discoverServices: nil];}/*** call failed to connect the peripherals */-(void) centralManager :( CBCentralManager *) central didDisconnectPeripheral :( CBPeripheral *) peripheral error :( NSError *) error {}# pragma makr-CBPeripheralDelegate/*** calls the ** @ param peripheral Service's peripherals */-(void) peripheral :( CBPeripheral *) as long as the service is scanned *) peripheral didDiscoverServices :( NSError *) error {// obtain all the services scanned by the peripherals. NSArray * services = peripheral. services; for (CBService * service in services) {// obtain the required service if ([service. UUID. UUIDString isEqualToString: @ 123]) {// find the desired feature from the required service // scan features from the service in peripheral [peripheral discoverCharacteristics: nil forService: service] ;}}/ *** as long as the feature is scanned, The ** @ param peripheral feature's peripherals * @ param service feature's services */-(void) will be called) peripheral :( CBPeripheral *) peripheral didDiscoverCharacteristicsForService :( CBService *) service error :( NSError *) error {// get all the special NSArray * characteristics = service in the service. characteristics; // traverses features to obtain the required feature processing for (CBCharacteristic * characteristic in characteristics) {if ([characteristic. UUID. UUIDString isEqualToString: @ 8888]) {NSLog (@ set the alarm) ;}}@ end