Bluetooth 4.0

Source: Internet
Author: User

1.3 Implementation Process

As a center to achieve complete communication, generally through the following steps:

Establish central role-scan peripherals (Discover)-connect peripherals (Connect)-scan services and features in peripherals (discover)-interact with peripherals (explore and interact)-disconnect (disconnect).

1.3.1 Establishing a central role

Centralmanager = [[Cbcentralmanageralloc]initwithdelegate:selfqueue:nil];

1.3.2 Scan Peripherals

[self. Centralmanagerscanforperipheralswithservices:niloptions:nil];

1.3.3 Connecting Peripherals

Bluetooth device callback function found

-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: (nsdictionary *) advertisementdata RSSI: (nsnumber *) rssi{}

1.3.4 scanning of services and features in peripherals (Discover)

When the connection succeeds, the system will tell us through the callback function, and then we will scan all the services and features under the device in this callback, the code is as follows:

Connection Successful

-(void) Centralmanager: (Cbcentralmanager *) Central didconnectperipheral: (cbperipheral *) Peripheral {}

Find a service

-(void) Peripheral: (cbperipheral *) aperipheral diddiscoverservices: (nserror *) error {}

Find a feature

-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (cbservice *) Service error: (Nserror *) {}

1.3.5 Data interaction with peripherals (explore and interact)

Write Data

-(void) Writechar: (NSData *) data{}

There are two types of data reading, one is direct reading (reading directly) and the other is subscription (subscribe).

The actual use of a specific application to see specific scenarios and characteristics of the properties themselves. What is the attribute of the characteristic itself, the first good understanding? The feature has a Properties field (Characteristic.properties), which is an integer value that has the following definitions:

enum  {

Cbcharacteristicpropertybroadcast = 0x01,

Cbcharacteristicpropertyread = 0x02,

Cbcharacteristicpropertywritewithoutresponse =0x04,

Cbcharacteristicpropertywrite = 0x08,

Cbcharacteristicpropertynotify = 0x10,

Cbcharacteristicpropertyindicate = 0x20,

Cbcharacteristicpropertyauthenticatedsignedwrites =0x40,

Cbcharacteristicpropertyextendedproperties =0x80,

};

For example, the feature you want to interact with, the value of its properties is 0x10, which means you can only receive data in the form of a subscription. I'm here to subscribe, the code to start the subscription is as follows:

Monitoring device

-(void) Startsubscribe

{

[_testperipheral Setnotifyvalue:yes forcharacteristic:_readcharacteristic];

}

When the device has data returned, it is also notified by a system callback, as follows:

-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) Characteristic error: (Nserror *) error

{

if(Error)

{

NSLog (@"error updating value for characteristic%@ error:%@", characteristic. UUID, [Error localizeddescription]);

if([_mainmenudelegate respondstoselector:@selector (didnotifyreaderror:)])

[_mainmenudelegate Didnotifyreaderror:error];

return;

}

[_recvdata AppendData:characteristic.value];

if([_recvdata length] >=5)//Received length

{

unsigned char*buffer = (unsignedchar*) [_recvdata bytes];

intnlen = buffer[3]*+ buffer[4];

if([_recvdata length] = = (nlen+2))

{

After receiving, notify the agent to do things

if([_mainmenudelegate respondstoselector:@selector (Didnotifyreaddata)])

[_mainmenudelegate Didnotifyreaddata];

}

}

}

1.3.6 Disconnect (disconnect)

Active Disconnect Device

-(void) DisConnect

Two. Process:

1. Determine the status of PowerOn, then perform a scan

2. Stop the scan and connect the peripherals

3. Successful connection, search service

4. Looking for features in the service

5. Adding notifications to features

5. If the notification is added successfully, then you can read the value[in real-time, that is, the agent calls this method as long as the peripheral sends the data [the frequency of the general peripheral is 10Hz].

6. Process the received Value,[hex value, get conversion]

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.