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 status

cbcentralmanagerstateunsupported, status not supported by the device

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. Manager scanforperipheralswithservices:nil options:nil];

< Span class= "S1" > You can set a timer to stop searching after a period of time to avoid power consumption   [ 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

can be based on the advertisementdata inside Kcbadvdataserviceuuids The value UUID to filter a device

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

connecting devices [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 the 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

here the device's The Services attribute has data for the device service, which can be traversed to match the service we need, this is usually negotiated with the hardware manufacturer, where we need UUID for FF00 service .

If the service is successfully matched, the calling method   [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, it's going to be a group. Cbcharacteristic, is a feature of the service, where we assume that sending data requires the use of a characteristic UUID as FF02

The data sent is   550504010101AA, we convert it to NSData type call method    [peripheral writevalue :d ata forcharacteristic :c type Cbcharacteristicwritewithoutresponse   Send data to 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 the device's data 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

This simply completes the Bluetooth 4.0

This is my first blog, but also the initial contact to Bluetooth, write a little messy, there are problems can be discussed with each other.

If there is to be reproduced please indicate the source.

iOS Bluetooth 4.0

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.