iOS蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)

BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx。

特色:

  • 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你更简单地使用CoreBluetooth API。
  • CoreBluetooth所有方法都是通过委托完成,代码冗余且顺序凌乱。BabyBluetooth使用block方法,可以重新按照功能和顺序组织代码,并提供许多方法减少蓝牙开发过程中的代码量。
  • 链式方法体,代码更简洁、优雅。
  • 通过channel切换区分委托调用,并方便切换

来源

最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。一整条链下来要近10几个委托方法,并且不断的在委托方法中调用方法再进入其他的委托,导致 代码很零散。因此我就想让coreBluetooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。

更新于:20150916,现在BabyBluetooth 已经有了96个star

更新于:20160129,现在BabyBluetooth 已经有了880个star

期待

  • 蓝牙库写起来很辛苦,希望大家可以多多支持,多多star。BabyBluetooth主页
  • 如果在使用过程中遇到BUG,或发现功能不够用,希望你能Issues我,谢谢
  • 期待大家也能一起为BabyBluetooth输出代码,这里我只是给BabyBluetooth开了个头,他可以增加和优化的地方还是非常多。也期待和大家在Pull Requests一起学习,交流,成长。

Quick Example

objc 复制代码
//导入.h文件和系统蓝牙库的头文件
#import "BabyBluetooth.h"

-(void)viewDidLoad {
    [super viewDidLoad];

   //初始化BabyBluetooth 蓝牙库
    baby = [BabyBluetooth shareBabyBluetooth];
     //设置蓝牙委托
     [self babyDelegate];
     //设置委托后直接可以使用,无需等待CBCentralManagerStatePoweredOn状态
     baby.scanForPeripherals().begin()
}

//蓝牙网关初始化和委托方法设置
-(void)babyDelegate{

    //设置扫描到设备的委托
    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"搜索到了设备:%@",peripheral.name);
    }];
    //设置设备连接成功的委托
    [baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
        NSLog(@"设备:%@--连接成功",peripheral.name);
    }];
    //设置发现设备的Services的委托
    [baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) {
        for (CBService *service in peripheral.services) {
            NSLog(@"搜索到服务:%@",service.UUID.UUIDString);
        }
    }];
    //设置发现设service的Characteristics的委托
    [baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
        NSLog(@"===service name:%@",service.UUID);
        for (CBCharacteristic *c in service.characteristics) {
            NSLog(@"charateristic name is :%@",c.UUID);
        }
    }];
    //设置读取characteristics的委托
    [baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
        NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value);
    }];
    //设置发现characteristics的descriptors的委托
    [baby setBlockOnDiscoverDescriptorsForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error) {
        NSLog(@"===characteristic name:%@",characteristic.service.UUID);
        for (CBDescriptor *d in characteristic.descriptors) {
            NSLog(@"CBDescriptor name is :%@",d.UUID);
        }
    }];
    //设置读取Descriptor的委托
    [baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) {
        NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value);
    }];

    //过滤器
    //设置查找设备的过滤器
    [baby setDiscoverPeripheralsFilter:^BOOL(NSString *peripheralsFilter) {
        //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
        if (peripheralsFilter.length >1) {
            return YES;
        }
        return NO;
    }];

    //设置连接的设备的过滤器
     __block BOOL isFirst = YES;
    [baby setFilterOnConnetToPeripherals:^BOOL(NSString *peripheralName) {
        //这里的规则是:连接第一个AAA打头的设备
        if(isFirst && [peripheralName hasPrefix:@"AAA"]){
            isFirst = NO;
            return YES;
        }
        return NO;
    }];


}

CoreBluetooch中实现上扫描,连接,发现服务和characteristic以及它的值相关方法调用是很麻烦啰嗦凌乱的。如下: centralManager启动->状态委托->调用扫描方法->进入扫描到设备的委托->调用连接设备方法->进入连接到设备的委托->发现服务方法->发现服务委托-> 发现characteristic方法->发现characteristic委托->读characteristic的value->读characteristic的value的委托->读description,读description的value-> ....的委托

而BabyBluetooth只需要一句话就执行了上面的内容。

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

另一方面,BabyBluetooth所有的委托方法都紧凑的聚在了一起。此外,快速示例中没有包括channel的使用,如果包括了channel,那么ios几个页面或者组件的蓝牙 调用模块都可以写在一起,看起来就觉得很方便。

关于更多BabyBluetooth的介绍和使用示例已经api,请移步到BabyBluetooth主页查看

相关推荐
报错小能手2 小时前
Swift EventBus讲解
开发语言·ios·swift
HiDev_5 小时前
iOS 蓝牙开发进阶:彻底理解 CBManager(状态、权限与正确使用方式)
ios·objective-c·蓝牙·ble
文件夹__iOS20 小时前
SwiftUI 核心选型:class + ObservableObject VS struct + @State
ios·swiftui·swift
SameX1 天前
独立开发了一款健康记录 App,聊聊几个让我纠结很久的设计决策
ios
报错小能手1 天前
Swift UI 框架 实战 简易计数器、待办清单 、随机壁纸图库、个人笔记
ui·ios
游戏开发爱好者81 天前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview
for_ever_love__2 天前
UI学习:多界面传值的正向传值(属性传值)和反向传值(代理传值)
学习·ui·ios·objective-c
开心就好20252 天前
全面介绍iOS开发工具:Xcode、AppCode、CocoaPods、Fastlane和Git
后端·ios
懋学的前端攻城狮2 天前
数据持久化与缓存策略:在离线与在线间架起桥梁
ios·swift
~央千澈~2 天前
以cocos3.8.8开发的游戏为例商业实战项目举例cocos打包ios苹果安装包ipa完整详细教程-优雅草卓伊凡
ios