First, go to the official documentation.
I was not responsible for the Bluetooth communication project recently, but my colleague in charge of the project had to bite his teeth to learn. Whining .....
Now we will summarize the key points of creating a Bluetooth project. Because the project mainly involves the central mode, we will only summarize the usage of the central mode.
1. Introduce CoreBluetooth. framework
2. Implement the Bluetooth protocol, such:
The. h file is as follows:
@ Protocol CBCentralManagerDelegate;
@ Protocol CBPeripheralDelegate;
@ Interface ViewController: UIViewController
The. m file is as follows:
# Import "CoreBluetooth/CoreBluetooth. h"
In addition, please add a proxy
3. The following is the process of making Bluetooth work.
3.1 create a CBCentralManager instance
Self. cbCentralMgr = [[CBCentralManageralloc] initWithDelegate: selfqueue: nil];
Set proxy, for example:
Self. cbCentralMgr. delegate = self;
Create an array to manage peripherals
Self. peripheralArray = [NSMutableArrayarray];
3.2 scan the surrounding Bluetooth
In fact, if the surrounding bluetooth device can be found, it will always send advertising messages. The central device is to receive these messages to find the surrounding Bluetooth
NSDictionary * dic = [NSDictionarydictionaryWithObjectsAndKeys: [NSNumbernumberWithBool: false], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[Self. cbCentralMgrscanForPeripheralsWithServices: niloptions: dic];
3.3 found a bluetooth device
That is to say, the CBCentralManager will notify the agent to process the advertisement information sent from a bluetooth device.
-(Void) centralManager :( CBCentralManager *) central didDiscoverPeripheral :( CBPeripheral *) peripheral advertisementData :( NSDictionary *) advertisementData RSSI :( NSNumber *) RSSI
{
}
If there are more than one Bluetooth around, this method will be called multiple times. You can print the Bluetooth information around it through tableView or other controls.
3.4 connect to a Bluetooth
[Self. cbCentralMgrconnectPeripheral: peripheraloptions: [NSDictionarydictionaryWithObject: [NSNumbernumberWithBool: YES] forKey: cbconnectperipheraloption1_yondisconnectionkey];
One central device can connect multiple peripheral Bluetooth devices at the same time
After a bluetooth device is connected, CBCentralManager notifies the proxy
-(Void) centralManager :( CBCentralManager *) central didConnectPeripheral :( CBPeripheral *) peripheral
{
}
In the future, we will obtain and communicate some information from the peripheral Bluetooth. Some events may be handled during these processes, so we need to set a proxy for this peripheral, for example:
Peripheral. delegate = self;
3.5 query the Bluetooth service
[Peripheral discoverServices: nil];
The returned Bluetooth service notification is implemented through proxy.
-(Void) peripheral :( CBPeripheral *) peripheral didDiscoverServices :( NSError *) error
{
For (CBService * servicein peripheral. services ){
}
}
3.6 query the feature value of a service
[Peripheral discoverCharacteristics: nilforService: service];
The returned Bluetooth feature value notification is implemented through proxy.
-(Void) peripheral :( CBPeripheral *) peripheral didDiscoverCharacteristicsForService :( CBService *) service error :( NSError *) error
{
For (CBCharacteristic * characteristicin service. characteristics ){
}
}
3.7 send data to Bluetooth
[Peripheral writeValue: dataforCharacteristic: characteristic type: CBCharacteristicWriteWithResponse];
A proxy event is triggered.
-(Void) peripheral :( CBPeripheral *) peripheral didWriteValueForCharacteristic :( CBCharacteristic *) characteristic error :( NSError *) error
{
}
3.8 process data sent from Bluetooth
-(Void) peripheral :( CBPeripheral *) peripheral didUpdateValueForCharacteristic :( CBCharacteristic *) characteristic error :( NSError *) error
{
}
3.9 retrievePeripheralsWithIdentifiers example
-(IBAction) Retrieve :( id) Sender
{
[Self. tvLogsetText: @ ""];
NSMutableArray * Identifiers = [NSMutableArrayarray];
For (CBPeripheral * peripheralin self. peripheralArray ){
[IdentifiersaddObject: peripheral. identifier];
}
[SelfaddLog: @ "[self. cbCentralMgr retrievePeripheralsWithIdentifiers: self. PeripheralIdentifiers]"];
Self. retrievePeripherals = [self. cbCentralMgrretrievePeripheralsWithIdentifiers: Identifiers];
For (CBPeripheral * peripheralin self. retrievePeripherals ){
[SelfaddLog: [NSStringstringWithFormat: @ "% @ name: % @", peripheral, peripheral. name];
}
[Self. tableViewPeripheralreloadData];
}
3.10 retrieveConnectedPeripheralsWithServices example
-(IBAction) Retrieve :( id) Sender
{
[Self. tvLogsetText: @ ""];
NSMutableArray * services = [NSMutableArrayarray];
For (CBPeripheral * peripheralin self. peripheralArray ){
If (peripheral. isConnected ){
For (CBService * servicein peripheral. services ){
[ServicesaddObject: service. UUID];
}
}
}
[SelfaddLog: @ "[self. cbCentralMgr retrieveConnectedPeripheralsWithServices: peripheral. services]"];
Self. retrievePeripherals = [self. cbCentralMgrretrieveConnectedPeripheralsWithServices: services];
For (CBPeripheral * peripheralin self. retrievePeripherals ){
[SelfaddLog: [NSStringstringWithFormat: @ "% @ name: % @", peripheral, peripheral. name];
}
[Self. tableViewPeripheralreloadData];
}
For this process, the parameter settings in the example, and some other proxies, Please study for yourself, because I am just getting started.
The example is here. For more information, see.