IOS, Bluetooth development!! --by, help Ray.

Source: Internet
Author: User

There are several ways to develop Bluetooth in iOS.

1 gamekit.framework

"Can only exist between iOS devices, more games

Can search the demo more, not exactly say the name, Code4app inside there is "

2 Corebluetooth.framework

"Bluetooth 4.0 must be supported, and above IPhone4, that is, at least 4s mobile phones. Data can be interacted with third-party devices,

The official demo is temperature Sensor.

3 Externalaccessory.framework

"Can interact with third-party Bluetooth devices, but the Bluetooth device must be MFI certified and requires an Apple protocol,

The official demo is Eademo and Btle"

4 Multipeer Connectivity.framework

"Can only be used between iOS devices, and iOS7 is only introduced. The main purpose is to share files, but the files are inside the sandbox

The official demo is iOS7 sample "

Below is a detailed description of how Bluetooth 4.0 BLE development mode is applied under iOS.

First, ble divides the Bluetooth device into two categories:

One central device (centrally)

Two peripheral devices (peripheral)

The two devices interact in the following ways:

First the peripherals broadcast their own information, and when the central device enables the discovery feature, the broadcast peripherals are discovered and a list of these peripherals is obtained.

Central Device Select the peripheral device you need to connect to. The first step in the interaction between the central device and the peripheral device was then opened.

Detailed analysis of the next steps such as:

The left side is the central device, and the right side is the peripheral device (peripheral).

Here the central connection peripheral, and send data to peripheral as an example, combined with code for analysis.

The steps are as follows:

1 Central device Find peripheral devices

Scan the Bluetooth around the surrounding Bluetooth scan

Nsdictionary * dic = [nsdictionary dictionarywithobjectsandkeys:[nsnumber numberwithbool:false], Cbcentralmanagerscanoptionallowduplicateskey, nil];

[Self.cbcentralmgr Scanforperipheralswithservices:nil Options:dic];

Find a Bluetooth

-(void) Centralmanager: (Cbcentralmanager *) Central diddiscoverperipheral: (cbperipheral *) Peripheral Advertisementdata: (nsdictionary *) advertisementdata Rssi: (NSNumber *) Rssi

{

}

2 Connect the peripheral you need to connect, here is the Cbperipheral object in.

[Self.cbcentralmgr connectperipheral:peripheral options:[nsdictionary Dictionarywithobject:[nsnumber Numberwithbool:yes] Forkey:cbconnectperipheraloptionnotifyondisconnectionkey];

when a Bluetooth is connected, Cbcentralmanager notifies the agent to process  

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

{

3 find the corresponding service and find the cbcharacteristic under the corresponding service.

[Peripheral Discoverservices:nil];

The returned Bluetooth service notification is implemented through the proxy

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

{

For (cbservice* service in peripheral.services) {}

[Peripheral Discovercharacteristics:nil Forservice:service];

The returned Bluetooth signature notification is implemented through the proxy

-(void) Peripheral: (cbperipheral *) Peripheral Diddiscovercharacteristicsforservice: (Cbservice *) Service error: ( Nserror *) Error

{

for (cbcharacteristic * characteristic in service.characteristics) {

}

}

4 Send data to the corresponding cbcharactieristic. There are 4 ways of sending data and receiving data.

[Peripheral Writevalue:data forcharacteristic:characteristic Type:cbcharacteristicwritewithresponse];

A proxy event is also triggered

-(void) Peripheral: (cbperipheral *) Peripheral didwritevalueforcharacteristic: (cbcharacteristic *) characteristic Error: (NSERROR *) error

{

Processing data sent by Bluetooth

-(void) Peripheral: (cbperipheral *) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic *) characteristic Error: (NSERROR *) error

{

}

Retrieveperipheralswithidentifiers Use Example

-(Ibaction) Retrieve: (ID) Sender

{

[Self.tvlog settext:@ ""];

Nsmutablearray * Identifiers = [Nsmutablearray array];

for (cbperipheral * peripheral in Self.peripheralarray) {

[Identifiers AddObject:peripheral.identifier];

}

}

IOS, Bluetooth development!! --by, help Ray.

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.