IOS Bluetooth-related-babybluetooth Bluetooth library introduction (4)

Source: Internet
Author: User

Babybluetooth is one of the most easy-to-use Bluetooth libraries, based on the Corebluetooth package, and compatible with iOS and Mac OSX.

Characteristics:
    • The lightweight, open Source library based on the native Corebluetooth framework package can help you use the Corebluetooth API more easily.
    • Corebluetooth all methods are done through a delegate, the code is redundant, and the order is messy. Babybluetooth uses the Block method to re-organize the code in function and order, and provides many ways to reduce the amount of code in the development of Bluetooth.
    • Chained method body, the code is more concise, elegant.
    • Use channel switching to differentiate delegate calls and facilitate switching
Source

In recent months have been doing Bluetooth project, with Corebluetooch feeling statement written everywhere, not elegant. A whole chain down to nearly 10 delegate methods, and constantly call methods in the delegate method to enter other delegates, resulting in the code is very fragmented. So I wanted to make corebluetooth use simpler and more elegant syntax, so start writing this Babybluetooch Bluetooth library.

Just started to write Babybluetooth, only the goal, it is not worth how to achieve, so step-by-step installation goals to achieve, but also encountered a lot of problems, slowly also came up with a solution, and step by step until the morning of September 7, before the completion of the first version can be used. For Bluetooth, a feature that does not appear high in apps, the Bluetooth-enabled library on GitHub uses up to 100 star, and then has 15 star and 10 fork in BABYBLUETOOCH2 days, which is pretty good, Soon more and more people are involved in the project, Issues,pull request and so on. These are very valuable resources for personal growth and project growth.

Update: 20150916, now Babybluetooth has 96 star

Expect
    • Bluetooth library is very hard to write, I hope you can support a lot, more star. Babybluetooth Home
    • If you encounter a bug in the process of use, or found that the function is not enough, I hope you can issues me, thank you
    • Look forward to everyone can also work together for Babybluetooth output code, here I just give Babybluetooth opened a head, he can add and optimize the place is still very much. Also look forward to and everyone in pull requests together learning, communication, growth.
Quick Example
Import the. h file and the header file of the System Bluetooth library#import "BABYBLUETOOTH.H"-(void)Viewdidload{[SuperViewdidload];Initializing the Babybluetooth Bluetooth libraryBaby=[BabybluetoothSharebabybluetooth];Set up a Bluetooth delegate[SelfBabydelegate];Can be used directly after setting the delegate without waiting for the Cbcentralmanagerstatepoweredon stateBaby.Scanforperipherals().Begin()}Bluetooth Gateway Initialization and delegation method settings-(void)Babydelegate{Set up a delegate to scan to a device[BabySetblockondiscovertoperipherals:^(Cbcentralmanager*Central,Cbperipheral*Peripheral,Nsdictionary*Advertisementdata,NSNumber*Rssi){NSLog(@ "Found device:%@",Peripheral.Name);}];Set up a delegate for a successful device connection[Babysetblockonconnected:^(Cbcentralmanager*Central,Cbperipheral*Peripheral){NSLog(@ "Device:%@--connection succeeded",Peripheral.Name);}];Set up a delegate that discovers services for a device[BabySetblockondiscoverservices:^(Cbperipheral*Peripheral,Nserror*Error){For(Cbservice*ServiceInchPeripheral.Services){NSLog(@ "Search to service:%@",Service.Uuid.Uuidstring);}}];Set up a delegate that discovers the service's characteristics[BabySetblockondiscovercharacteristics:^(Cbperipheral*Peripheral,Cbservice*Service,Nserror*Error){NSLog(@ "===service name:%@",Service.Uuid);For(Cbcharacteristic*CInchService.Characteristics){NSLog(@ "charateristic name is:%@",C.Uuid);}}];Set delegate to read characteristics[BabySetblockonreadvalueforcharacteristic:^(Cbperipheral*Peripheral,Cbcharacteristic*Characteristics,Nserror*Error){NSLog(@ "Characteristic name:%@ value is:%@",Characteristics.Uuid,Characteristics.Value);}];Set up a delegate that discovers characteristics descriptors[BabySetblockondiscoverdescriptorsforcharacteristic:^(Cbperipheral*Peripheral,Cbcharacteristic*Characteristic,Nserror*Error){NSLog(@ "===characteristic name:%@",Characteristic.Service.Uuid);For(Cbdescriptor*DInchCharacteristic.Descriptors){NSLog(@ "Cbdescriptor name is:%@",D.Uuid);}}];Set delegate to read descriptor[BabySetblockonreadvaluefordescriptors:^(Cbperipheral*Peripheral,Cbdescriptor*Descriptor,Nserror*Error){NSLog(@ "Descriptor name:%@ value is:%@",Descriptor.Characteristic.Uuid,Descriptor.Value);}];Filter filtersSet up filters for finding devices[BabySetdiscoverperipheralsfilter:^BOOL(NSString*Peripheralsfilter){Set lookup rule is name greater than 1, the search rule is peripheral.name length > 1If(Peripheralsfilter.Length>1){ReturnYES;}ReturnNO;}];Set up filters for connected devices__blockBOOLIsFirst=YES;[Babysetfilteronconnettoperipherals:^BOOL  (nsstring *peripheralname) Span class= "P" >{//the rules here are: Connect the first AAA-preceded device if ( IsFirst && [peripheralname hasprefix< Span class= "p" >:@ "AAA" ] {isfirst = noreturn yes} return no}];                /span>                

It is cumbersome to corebluetooch to implement on-scan, connect, discover service and characteristic and its value-related method calls. As follows: Centralmanager start, state delegation, call scan method, incoming scan to device delegate, call connection device method, enter a delegate, Discovery services method, and so on to connect to a device, discovery services Found characteristic method--characteristic delegate--read characteristic value-> read characteristic Read description, read description's value-> .... The delegate

and Babybluetooth only need a sentence to execute the above content.

  //扫描设备 然后读取服务,然后读取characteristics名称和值和属性,获取characteristics对应的description的名称和值  baby.scanForPeripherals().connectToPeripheral().discoverServices() .discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic() .readValueForDescriptors().begin();

On the other hand, all of the babybluetooth are clustered together in a compact way. In addition, the quick example does not include the use of the channel, and if the channel is included, then the Bluetooth calling module of several iOS pages or components can be written together to make it seem convenient.

IOS Bluetooth-related-babybluetooth Bluetooth library introduction (4)

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.