Original link: http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&plg_usr =1&plg_vkey=1&plg_nld=1&plg_d
Article source Http://blog.csdn.net/xgcyangguang
What is done is to connect via mobile phone/pad with Bluetooth 4.0 device, then press the corresponding button on the device, we will receive the corresponding value. First need and do the Bluetooth 4.0 colleague Communication Good communication protocol, the specific data analysis part is not too much to repeat, mainly write about Bluetooth receive data part
1. Communicate with your Bluetooth colleague about the UUID and features of your device, and you can write them as macros
#define TRANSFER_SERVICE_UUID @ "0000FFF0-0000-1000-8000-00805F9B34FB" #define Transfer_characteristic_uuid @ "0000FFF7-0000-1000-8000-00805F9B34FB"
2. Import two header files in an. h file and implement two protocols in the interface
#import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> #import "UUID.h"//need to implement protocol @interface Viewcontroller () < Cbcentralmanagerdelegate, cbperipheraldelegate>{}
3. Create two Bluetooth device properties, one equivalent to the host, one equivalent to the peripheral slave
#pragma mark Bluetooth device @property (strong, nonatomic) Cbcentralmanager *centralmanager; Receive @property (strong, nonatomic) cbperipheral *discoveredperipheral; Peripheral @end
4. Start bluetooth configuration
#pragma mark sets itself as Agent-(void) viewdidload {[Super Viewdidload] After the initialization interface is finished; _centralmanager = [[Cbcentralmanager alloc] initwithdelegate:self queue:nil];} #pragma mark here to monitor the central state value, you can determine whether Bluetooth is on/available-(void) Centralmanagerdidupdatestate: (Cbcentralmanager *) central{ nsmutablestring *stringforcentralmanagerstate = [nsmutablestring stringwithstring:@ "UpdateState:"]; Switch (central.state) {case Cbcentralmanagerstateunknown: [Stringforcentralmanagerstate appendstring:@ "Unkown\n"]; Break Case cbcentralmanagerstateunsupported: [stringforcentralmanagerstate appendstring:@ "unsupported\n"]; Case cbcentralmanagerstateunauthorized: [stringforcentralmanagerstate appendstring:@ "Unauthorized\n"]; Case cbcentralmanagerstateresetting: [stringforcentralmanagerstate appendstring:@ "resetting\n"]; Case Cbcentralmanagerstatepoweredoff: [stringforcentralmanagerstate appendstring:@ "poweroff\n"]; Case Cbcentralmanagerstatepoweredon://Device supports BLE and available [stringforcentralmanagerstate appendstring:@ " Poweredon\n "]; Start search [self scan]; Break Default: [Stringforcentralmanagerstate appendstring:@ "none\n"]; Break } NSLog (@ "%@", Stringforcentralmanagerstate);} #pragma mark Scan-(void) scan{//The first parameter, if set to nil, will look for all service [Self.centralmanager scanforperipheralswithservices:@[ [Cbuuid Uuidwithstring:transfer_service_uuid]] options:@{Cbcentralmanagerscanoptionallowduplicateskey: @YES}]; NSLog (@ "scanning started");} #pragma mark discovers that the device, connection//Once a compliant device is found, will callback this method-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral :(cbperipheral *) Peripheral Advertisementdata: (nsdictionary *) advertisementdata RSSI: (NSNumber *) RSSI{NSLog (@ "Disc overed%@ at%@ ", Peripheral.name, RSSI); if (self.discoveredperipheral! = peripheral) {Self.discovEredperipheral = peripheral; Connection NSLog (@ "Connecting to peripheral%@", peripheral); [Self.centralmanager connectperipheral:peripheral Options:nil]; }} #pragma mark failed to connect processing method-(void) Centralmanager: (Cbcentralmanager *) Central didfailtoconnectperipheral: (cbperipheral *) Peripheral Error: (NSERROR *) error{NSLog (@ "Failed to connect to%@. (%@) ", peripheral, [error localizeddescription]);//[Self cleanup];} #pragma mark when connecting the device-(void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (Cbperipheral *) peripheral{NSLog (@ "Peripheral Connected"); The device is connected, so stop searching [Self.centralmanager Stopscan]; NSLog (@ "scanning stopped"); Make sure we get the discovery callbacks peripheral.delegate = self; Find SERVICE for specified UUID [peripheral discoverservices:@[[cbuuid uuidwithstring:transfer_service_uuid]];} #pragma mark discovers that the specified service on the device will callback here-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nserror *) error{ if (ErroR) {NSLog (@ "Error Discovering services:%@", [Error localizeddescription]); Return }//Find characteristic for the specified uuid (Cbservice *service in peripheral.services) {[Peripheral Discovercharac Teristics:@[[cbuuid Uuidwithstring:transfer_characteristic_uuid]] forservice:service]; }} #pragma mark finds the characteristic of the specified UUID callback here-(void) Peripheral: (Cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (cbservice *) Service error: (NSERROR *) error{if (error) {NSLog (@ "Error dis Covering characteristics:%@ ", [Error localizeddescription]); Return } for (Cbcharacteristic *characteristic in service.characteristics) {if ([characteristic. UUID Isequal:[cbuuid Uuidwithstring:transfer_characteristic_uuid]) {NSLog (@ "Find the characteristic"); [Peripheral Setnotifyvalue:yes forcharacteristic:characteristic]; }}}-(void) Peripheral: (Cbperipheral *) Peripheral DIdupdatevalueforcharacteristic: (cbcharacteristic *) characteristic error: (Nserror *) error{if (error) { NSLog (@ "Error discovering Characteristics:%@", [Error localizeddescription]); Return } NSLog (@ "value-to-%@", characteristic.value); #pragma mark intercepts the received data//Here we can get the value values to parse the data nsdata *data = Characteristic.value; }
#pragma mark automatically re-connects after Bluetooth disconnects-(void) Centralmanager: (Cbcentralmanager *) Central diddisconnectperipheral: (Cbperipheral *) Peripheral Error: (NSERROR *) error{ NSLog (@ "peripheral disconnected"); Self.discoveredperipheral = nil; [Self scan]; Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Bluetooth connection Status" message:@ "Connection disconnected" Delegate:nil cancelbuttontitle:@ "OK "otherbuttontitles:@" closed ", nil]; [Alert show]; }
5. Bluetooth background Operation
To implement Bluetooth 4.0 can still work when the app enters the background, transfer data without writing code, only need to modify the Xxx-info.plist file
Add two items to the desired background mode
sharing data using Corebluetooth applicationsAnd
application for communication using CorebluetoothCan
Corebluetooth--ios Bluetooth 4.0 Usage tips