IOS Bluetooth development-CoreBluetooth details, ioscorebluetooth

Source: Internet
Author: User

IOS Bluetooth development-CoreBluetooth details, ioscorebluetooth

CoreBluetooth APIs are based on the BLE4.0 standard. This framework covers all the details of the BLE standard. Only new iOS devices and Mac devices are compatible with the BLE standard. in the CoreBluetooth framework, there are two main roles: Peripheral and Central. The entire framework is designed around these two main roles, and there are a series of callback data exchanges between them. 1 shows the relationship between the surrounding area and the Central area.

There are two modes for Developing Bluetooth. One is the center mode and the other is the peripheral mode. Here we mainly talk about the central mode. The process of the central mode is mainly divided into the following steps: 1. Establish a central device 2. Scan external devices 3. Connect to external devices 4. Scan services and features of external devices 5. send and receive data using related features and external devices. Before proceeding, we learned that a central device can connect to multiple external devices. One external device contains one or more services, and one service contains one or more features.

  

The following describes how to use Bluetooth as a beginner:

1. First, import the framework # import <CoreBluetooth/CoreBluetooth. h>,

Then create a central device and set up a proxy (do not forget to sign the Agreement ):

CBCentralManager * manager = [[CBCentralManageralloc] init];

Self. manager = manager;

Once the proxy is set to run the program, a required method in the protocol will be called:

-(Void) centralManagerDidUpdateState :( CBCentralManager *) central;

This method is to check whether the center device has Bluetooth enabled.

 

2. Scan external devices using the central device:

[Manager scanForPeripheralsWithServices: niloptions: nil];

The first parameter indicates scanning external devices with related services. For example, enter @ [[cbuuiduidwithstring: @ "UUID of the service of the external device to be connected"], indicates the external device with the UUID of the service of the external device to be connected, and nil indicates scanning all devices;

For details about options, you can enter a parameter such as @ {CBCentralManagerScanOptionAllowDuplicatesKey: @ YES}. YES indicates that the central device will continuously listen to messages from external devices. NO means NO.

Once an external device is scanned, it enters the Protocol

 

-(Void) centralManager :( CBCentralManager *) central didDiscoverPeripheral :( CBPeripheral *) peripheral advertisementData :( NSDictionary *) advertisementData Arg :( NSNumber *) RSSI;

In this method, we can filter the obtained hardware based on certain conditions, and connect to the external device that we need to connect to, for example, the external device with the name:

If ([peripheral. namehasPrefix: @ "A"]) {

// Connect the device

[Manager connectPeripheral: peripheraloptions: nil];

}

 

3. The statement in the if judgment is to connect the central device to an external device. If the connection succeeds or fails

-(Void) centralManager :( CBCentralManager *) central didConnectPeripheral :( CBPeripheral *) peripheral; (connection successful)

-(Void) centralManager :( CBCentralManager *) central didFailToConnectPeripheral :( CBPeripheral *) peripheral error :( nullableNSError *) error; (connection failed)

 

4. In the successful connection method, we start to scan external device services:

 

[Peripheral discoverServices: nil];

Next, you will jump into the proxy method of the service discovery:

 

-(Void) peripheral :( CBPeripheral *) peripheral didDiscoverServices :( NSError *) error;

In this method, we start to scan the features in the service:

 

[Peripheral discoverCharacteristics: nilforService: service];

When we scan features, we will jump into the Protocol Method for feature discovery:

 

-(Void) peripheral :( CBPeripheral *) peripheral didDiscoverCharacteristicsForService :( CBService *) service error :( NSError *) error;

 

5. After scanning the features, we can obtain the corresponding features for read/write operations.

For example, to read data:

 

If ([characteristics. UUID. UUIDStringisEqualToString: @ "UUID of the feature you need"]) {

// Read feature data

[Peripheral readValueForCharacteristic: characteristics];

}

This reads the information contained in the feature. As long as the feature is read, it enters another method:

 

-(Void) peripheral :( CBPeripheral *) peripheral didUpdateValueForCharacteristic :( CBCharacteristic *) characteristic error :( NSError *) error;

In this method, we can get the data we need. The write operation is

[PeripheralwriteValue: data-type data forCharacteristic: used feature type: CBCharacteristicWriteWithResponse];

The last type has two types: CBCharacteristicWriteWithResponse and CBCharacteristicWriteWithoutResponse;

Select the first data entry.

 

-(Void) peripheral :( CBPeripheral *) peripheral didWriteValueForCharacteristic :( CBCharacteristic *) characteristic error :( NSError *) error;

This method will tell us whether the write is successful, but if we don't have to consider whether the data written to the hardware is successful or not, select the second type.

The above is a brief introduction to the development process of Bluetooth in iOS, Which is easy for beginners to understand and understand. Of course, the Bluetooth development in the project is not so simple and needs to be considered based on the actual situation, but everything has to begin with the simplest. I have just started to learn about the bluetooth module. I have something wrong with it. Please point out that you can communicate with me if you cannot understand it.

 

Related Article

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.