General flow of iOS Bluetooth calls

Source: Internet
Author: User

Article turned from: http://www.cnblogs.com/ctaodream/archive/2013/07/03/3169962.html

One, the service side (also called peripheral equipment bar. Translation of brain remnants)

1. The implementation class must comply with the Protocol Cbperipheralmanagerdelegate

2. The main classes required are:

@property (strong,nonatomic) Cbperipheralmanager *peripheramanager;

@property (strong,nonatomic) cbmutablecharacteristic *customercharacteristic;

@property (strong,nonatomic) Cbmutableservice *customerservice;

3. There are comments in the calling process code

////VIEWCONTROLLER.M//Bluetoothdemo////Created by Psh_chen_tao on 7/3/13.//Copyright (c) Wolfman. All rights reserved.// #import "ViewController.h"StaticNSString *ConstKcharacteristicuuid =@"Cce62c0f-1098-4cd0-adfa-c8fc7ea2ee90"; StaticNSString *ConstKserviceuuid =@"50bd367b-6b17-4e81-b6e9-f62016f26e7b";@interfaceViewcontroller ()@end @implementationViewcontroller@synthesizePeripheramanager;@synthesizecustomercharacteristic;@synthesizeCustomerService;- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //the proxy is called directly after initialization-(void) Peripheralmanagerdidupdatestate: (Cbperipheralmanager *) PeripheralPeripheramanager=[[Cbperipheralmanager alloc]initwithdelegate:self Queue:nil]; //[Peripheramanager Startadvertising:nil];     } -(void) setup{Cbuuid*characteristicuuid =[Cbuuid Uuidwithstring:kcharacteristicuuid]; Customercharacteristic=[[Cbmutablecharacteristic alloc]initwithtype:characteristicuuid Properties:cbcharacteristicpropertynotify    Value:nil permissions:cbattributepermissionsreadable]; Cbuuid*serviceuuid =[Cbuuid Uuidwithstring:kserviceuuid]; CustomerService=[[Cbmutableservice alloc]initwithtype:serviceuuid Primary:yes];    [CustomerService Setcharacteristics:@[characteristicuuid]]; //when added, the agent is called-(void) Peripheralmanager: (Cbperipheralmanager *) Peripheral Didaddservice: (Cbservice *) Service error :(Nserror *) Error[Peripheramanager Addservice:customerservice]; } - (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.} #pragmaMark--cbperipheralmanagerdelegate-(void) Peripheralmanagerdidupdatestate: (Cbperipheralmanager *) peripheral{Switch(peripheral.state) {//in this case, you can call the Setup method (custom) when the Bluetooth setting is enabled.         CaseCbperipheralmanagerstatepoweredon:nslog (@"powered on");            [Self setUp];  Break;  CaseCbperipheralmanagerstatepoweredoff:nslog (@"powered off");  Break; default:             Break; }}   - (void) Peripheralmanager: (Cbperipheralmanager *) Peripheral Didaddservice: (Cbservice *) Service error: (Nserror *) error{if(Error = =Nil) {        //After you add a service, you can make a notification to the outside world after calling this method, the proxy's//(void) peripheralmanagerdidstartadvertising: (Cbperipheralmanager *) Peripheral Error: (NSERROR *) Error[Peripheramanager Startadvertising:@{cbadvertisementdatalocalnamekey:@"Service", Cbadvertisementdataserviceuuidskey: [Cbuuid Uuidwithstring:kserviceuuid]}]; }     } - (void) Peripheralmanagerdidstartadvertising: (Cbperipheralmanager *) Peripheral Error: (NSERROR *) error{NSLog (@"In peripheralmanagerdidstartadvertisiong:error");}@end
View Code

Second, the client (also called Center equipment Bar)

1. Implementing classes to comply with agreements <CBCentralManagerDelegate,CBPeripheralDelegate>

2. The main classes used are

@property (strong,nonatomic) Cbcentralmanager *centralmanager;

@property (strong,nonatomic) Nsmutabledata *mutabledata;

@property (strong,nonatomic) cbperipheral *peripheral;

3. General process

////VIEWCONTROLLER.M//bluetoothclient////Created by Psh_chen_tao on 7/3/13.//Copyright (c) Wolfman. All rights reserved.//#import "ViewController.h"StaticNSString *ConstKcharacteristicuuid =@"Cce62c0f-1098-4cd0-adfa-c8fc7ea2ee90";StaticNSString *ConstKserviceuuid =@"50bd367b-6b17-4e81-b6e9-f62016f26e7b";@interfaceViewcontroller ()@end@implementationViewcontroller@synthesizeCentralmanager;@synthesizeMutabledata;@synthesizeperipheral;- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //the proxy cbcentralmanagerdelegate-(void) centralmanagerdidupdatestate is called after initialization: (Cbcentralmanager *) CentralCentralmanager =[[Cbcentralmanager alloc]initwithdelegate:self Queue:nil]; }- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark--cbcentralmanagerdelegate-(void) Centralmanagerdidupdatestate: (Cbcentralmanager *) central{Switch(central.state) {//The judging state starts scanning around the device the first parameter is empty and all the connected devices are scanned you can//Specify a Cbuuid object to scan only devices registered with the specified service//when the Scanforperipheralswithservices method call is finished, the proxy cbcentralmanagerdelegate is called.//-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: (nsdictionary *) advertisementdata Rssi: (NSNumber *) Rssi method         CaseCbcentralmanagerstatepoweredon: [Centralmanager scanforperipheralswithservices:@[[cbuuid UUIDWithString:kS                                    ERVICEUUID]] Options:@{cbcentralmanagerscanoptionallowduplicateskey:yes}];  Break; default:             Break; }}- (void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: ( Nsdictionary *) Advertisementdata RSSI: (NSNumber *) rssi{if(peripheral) {self.peripheral=peripheral; //a device can be connected after it is discovered call the agent Cbcentralmanagerdelegate-(void) Centralmanager: (Cbcentralmanager *) Central when the method is finished calling Didconnectperipheral: (cbperipheral *) peripheral indicates that the connection has been set//If no connection is called-(void) Centralmanager: (Cbcentralmanager *) Central didfailtoconnectperipheral: (Cbperipheral *) Peripheral Error: (NSERROR *) Error[Centralmanager connectperipheral:peripheral options:@{cbconnectperipheraloptionnotifyonconnectionkey:yes}]; }}- (void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (Cbperipheral *) peripheral{NSLog (@"has connected"); [Mutabledata setLength:0]; Self.peripheral.Delegate=Self ; //Now that the device is connected, all you have to do is find the specified service on the device call the agent Cbperipheraldelegate (the method that calls the other proxy) after the method is called.//-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nserror *) Error[Self.peripheral Discoverservices:@[[cbuuid Uuidwithstring:kserviceuuid]]; }- (void) Centralmanager: (Cbcentralmanager *) Central didfailtoconnectperipheral: (cbperipheral *) Peripheral Error: (NSERROR *) error{//connection error occurs at this timeNSLog (@"Connected Periphheral failed");}#pragmaMark--cbperipheraldelegate-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (Nserror *) error{if(error==Nil) {        //In This method we'll find the service we need and call the Discovercharacteristics method to find the features we need.//when the Discovercharacteristics method call is complete, the proxy cbperipheraldelegate is called.//-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: ( Nserror *) Error         for(Cbservice *serviceinchperipheral.services) {if([Service. UUID Isequal:[cbuuid Uuidwithstring:kserviceuuid]) {[Peripheral discovercharacteristics:@[[cbuuid UUIDWit            HSTRING:KCHARACTERISTICUUID]] forservice:service]; }        }    }}- (void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: (NSError *) error{if(error==Nil) {        //In this method we need to find the features of the service we need and then call the Setnotifyvalue method to tell us to monitor the state change of this service feature//when the Setnotifyvalue method call is called, the proxy cbperipheraldelegate-(void) Peripheral: (Cbperipheral *) Peripheral Didupdatenotificationstateforcharacteristic: (cbcharacteristic *) characteristic error: (Nserror *) Error         for(Cbcharacteristic *characteristicinchservice.characteristics) {if([Characteristic.  UUID Isequal:[cbuuid Uuidwithstring:kcharacteristicuuid]) {[Peripheral Setnotifyvalue:yes            Forcharacteristic:characteristic]; }        }    }}- (void) Peripheral: (cbperipheral *) Peripheral didupdatenotificationstateforcharacteristic: (CBCharacteristic *) Characteristic error: (Nserror *) error{if(error==Nil) {        //calling the following method will call the-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: ( Cbcharacteristic *) Characteristic error: (Nserror *) Error[Peripheral readvalueforcharacteristic:characteristic]; }}- (void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic error: ( Nserror *) error{}@end
View Code

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.