iOS Bluetooth 4.0

Source: Internet
Author: User

iOS Bluetooth is used in the Corebluetooth framework

First import the framework

#import <CoreBluetooth/CoreBluetooth.h>

We need a manager to manage Bluetooth devices,Cbcentralmanager

Create a manager first

Self. Manager = [[cbcentralmanager alloc]initwithdelegate:self queue: nil options:nil];

Here only need to set up a proxy, the queue according to the needs of the choice, where nil is the default in the main thread. Options are the criteria for filtering the device.

Agent method is executed after manager is created

-(void) Centralmanagerdidupdatestate: (cbcentralmanager *) Central

You can view one of the manager's properties: state to identify the status of Bluetooth, which is an enumeration value cbcentralmanagerstate

typedef Ns_enum(Nsinteger, cbcentralmanagerstate) {

Cbcentralmanagerstateunknown = 0, the initial time is unknown (just when it was created)

Cbcentralmanagerstateresetting, resetting state

cbcentralmanagerstateunsupported, device not supported status

cbcentralmanagerstateunauthorized, device not authorized status

Cbcentralmanagerstatepoweredoff, device off state

Cbcentralmanagerstatepoweredon, device on state -- available status

};

If you are in a Cbcentralmanagerstatepoweredon state, you can start searching for Devices [self. Managerscanforperipheralswithservices:nil options:nil];

You can set a timer to stop searching after a period of time to avoid draining [self. Manager Stopscan];

If a device is found, the proxy method is called

-(void) Centralmanager: (cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: (nsdictionary<nsstring *,ID> *) advertisementdata Rssi: (nsnumber *) rssi

Where peripheral is the search device,advertisementdata is some information about the device, RSSI is the signal strength of the device

You can filter a device based on the value UUID of the kcbadvdataserviceuuids inside the advertisementdata.

After searching for the device, you can connect, and Bluetooth 4.0 supports one-to-many connections.

Connect device [self.  Manager connectperipheral:p eripheral options:nil];

Agent method is executed after successful connection

-(void) Centralmanager: (cbcentralmanager *) Central didconnectperipheral: (cbperipheral *) Peripheral

If you need to disconnect a device, call the method [self. Manager cancelperipheralconnection:p eripheral];

The agent method is called when the device disconnects itself or manually

-(void) Centralmanager: (cbcentralmanager *) Central diddisconnectperipheral: (cbperipheral *) Peripheral error: (nserror *) error

Here you can tell if it is not a manually disconnected connection, you can reconnect the disconnected device, realize the function of disconnecting the device automatically

Cbcentralmanager is used to do these things, to send data to the device and accept data to use cbperipheral

First set up the agent for the device

Peripheral. delegate = self;

Then start searching for device services [peripheral discoverservices:nil];

Agent method is executed after successful search

-(void) Peripheral: (cbperipheral *) Peripheral diddiscoverservices: (nserror *) error

In the service properties of the device there is the device services data, you can traverse this array to match to the services we need, this is generally negotiated with the hardware manufacturer, here we need UUID for FF00 service

If the service is successfully matched, then the method is called [peripheral discovercharacteristics:nil forservice: s];

The agent method is executed after success

-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (cbservice *) Service error: (nserror *) error

In this method, the properties of the service characteristics, will be a set of cbcharacteristic, is a feature of the service, here, we assume that the sending data need to use the characteristics of the UUID for FF02

The data sent is 550504010101AA, and we convert it to the NSData type call method [ peripheralwritevalue:d ataforcharacteristic: C C7>type:cbcharacteristicwritewithoutresponse] sending data to the device

The proxy method is executed when the send is successful

-(void) Peripheral: (cbperipheral *) Peripheral didwritevalueforcharacteristic: ( Cbcharacteristic *) characteristic error: (nserror *) error

If you are reading data from a device then use the method [peripheral readvalueforcharacteristic: c], the proxy method is executed after reading the data

-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: ( Cbcharacteristic*) characteristic error: (nserror *) error

iOS Bluetooth 4.0

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.