The easiest way to use Bluetooth (BLE )in ios,even bady can use.

Overview

The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx.

为什么使用它?

  • 1:基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你更简单地使用CoreBluetooth API。
  • 2:CoreBluetooth所有方法都是通过委托完成,代码冗余且顺序凌乱。BabyBluetooth使用block方法,可以重新按照功能和顺序组织代码,并提供许多方法减少蓝牙开发过程中的代码量。
  • 3:链式方法体,代码更简洁、优雅。
  • 4:通过channel切换区分委托调用,并方便切换
  • 5:便利的工具方法
  • 6:完善的文档,且项目处于活跃状态,不断的更新中
  • 7:github上star最多的纯Bluetooth类库
  • 8:包含多种类型的demo和ios蓝牙开发教程
  • 9:同时支持蓝牙设备中心模式和外设模式(central model and peripheral model)

当前版本 0.7.0

更新说明

详细文档请参考wiki The full documentation of the project is available on its wiki.

english readme link,please click it!

Table Of Contents

QuickExample

中心模式 central model

app作为中心,连接其他BLE4.0外设

1 if (peripheralName.length >1) { return YES; } return NO; }]; //....... } ">
//导入.h文件和系统蓝牙库的头文件
#import "BabyBluetooth.h"
//定义变量
BabyBluetooth *baby;

-(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 setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {
        //最常用的场景是查找某一个前缀开头的设备 most common usage is discover for peripheral that name has common prefix
        //if ([peripheralName hasPrefix:@"Pxxxx"] ) {
        //    return YES;
        //}
        //return NO;
        //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
        if (peripheralName.length >1) {
            return YES;
        }
        return NO;
    }];

    //.......
}

更多蓝牙操作方法和委托请参考wiki

中心模式使用示例请参考:BabyBluetoothAppDemo

外设模式 peripheral model

app模拟一个,BLE4.0外设,可以被其他设备连接和使用

模拟一个有2个service和6个characteristic的外设

//导入.h文件和系统蓝牙库的头文件
#import "BabyBluetooth.h"
//定义变量
BabyBluetooth *baby;

-(void)viewDidLoad {
    [super viewDidLoad];

    //配置第一个服务s1
    CBMutableService *s1 = makeCBService(@"FFF0");
    //配置s1的3个characteristic
    makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//
    makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//
    makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//可读写,uuid自动生成
    makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段
    makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段
    //配置第一个服务s2
    CBMutableService *s2 = makeCBService(@"FFE0");
    makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读
   
    //实例化baby
    baby = [BabyBluetooth shareBabyBluetooth];
    //配置委托
    [self babyDelegate];
    //启动外设
    baby.bePeripheral().addServices(@[s1,s2]).startAdvertising();
}

//设置蓝牙外设模式的委托
-(void)babyDelegate{

     //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) {
        NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state);
    }];
    
    //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) {
        NSLog(@"didStartAdvertising !!!");
    }];
    
    //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) {
        NSLog(@"Did Add Service uuid: %@ ",service.UUID);
    }];

    //.....
}

更多蓝牙外设模式委托请参考wiki

中心模式使用示例请参考:BluetoothStubOnIOS

如何安装

##1 手动安装 step1:将项目Classes/objc 文件夹中的文件直接拖入你的项目中即可

step2:导入.h文件

#import "BabyBluetooth.h"

##2 cocoapods step1:add the following line to your Podfile:

pod 'BabyBluetooth','~> 0.7.0'

step2:导入.h文件

#import "BabyBluetooth.h"

如何使用

用法请见wiki

示例程序说明

BabyBluetoothExamples/BabyBluetoothAppDemo :一个类似lightblue的程序,蓝牙操作全部使用BabyBluetooth完成。 功能:

  • 1:扫描周围设备
  • 2:连接设备,扫描设备的全部services和characteristic
  • 3:显示characteristic,读取characteristic的value,和descriptors以及Descriptors对应的value
  • 4:写0x01到characteristic
  • 5:订阅/取消订阅 characteristic的notify

BabyBluetoothExamples/BluetoothStubOnIOS : 一个iOS程序,启动后会用手机模拟一个外设,提供2个服务和若干characteristic。 该程序作为Babybluetooth 外设模式使用的示例程序

BabyBluetoothExamples/BabyBluetoothOSDemo :一个mac os程序,因为os和ios的蓝牙底层方法都一样,所以BabyBluetooth可以ios/os通用。但是os程序有个好处就是直接可以在mac上跑蓝牙设备,不像ios,必须要真机才能跑蓝牙设备。所以不能真机调试时可以使用os尝试蓝牙库的使用。

功能:

  • 1:扫描周围设备、连接设备、显示characteristic,读取characteristic的value,和descriptors以及Descriptors对应的value的委托设置,并使用nslog打印信息。

BabyBluetoothExamples/BluetoothStubOnOSX :一个mac os程序,该程序可以作为蓝牙外设使用,解决学习蓝牙时没有外设可用的囧境,并且可以作为peripheral model模式的学习示例。改程序用swift编码。

功能:

  • 1:作为蓝牙外设使用,可以被发现,连接,读写,订阅
  • 2:提供1个service,包含了3个characteristic,分别具有读、读写、订阅功能

兼容性

  • 蓝牙4.0,也叫做ble,ios6以上和iPhone4s以上可以自由使用
  • os和ios通用
  • 蓝牙设备相关程序必须使用真机才能运行。如果不能使用真机调试的情况,可以使用os程序调试蓝牙。可以参考示例程序中的BabyBluetoothOSDemo
  • 本项目和示例程序是使用ios 8.3开发,使用者可以自行降版本,但必须大于6.0

后期更新

已经更新的版本说明,请在wiki中查看

蓝牙学习资源

温馨提示:零基础做蓝牙开发很困难,所以就算使用babybluetooth降低了代码量,仍然任很有必要花上几天时间把蓝牙的基础概念弄明白后才开始动手~

组织和交流

  • qq交流群6: 284341984(满)
  • qq交流群5: 426082944(满)
  • qq交流群4: 313084771(满)
  • qq交流群3:530142592(满)
  • qq交流群2:168756967(满)
  • qq交流群1:426603940(满)
  • 个人微信公众号

个人微信公众号

招聘

 阿里云-iot事业部招人,p6 p7 前端,android,iOS,风光无限 一起造作,有兴趣的投简历到 [email protected]

期待

  • 蓝牙库写起来很辛苦,不要忘记点击右上角小星星star和follow支持一下~
  • 如果在使用过程中遇到BUG,或发现功能不够用,希望你能Issues我,谢谢
  • 如果你的app用到了Babybluetooth,希望你能发邮件([email protected])告诉我,我会在readme中展现出来。
  • 期待大家也能一起为BabyBluetooth输出代码,这里我只是给BabyBluetooth开了个头,他可以增加和优化的地方还是非常多。也期待和大家在Pull Requests一起学习,交流,成长。
Comments
  • 程式執行5秒後所有掃描都會停止

    程式執行5秒後所有掃描都會停止

    我在第一次成功連接後完成資料傳送並cancelNotify和在setBlockOnDidUpdateNotificationStateForCharacteristic執行[weakBaby cancelPeripheralConnection:self.currPeripheral]; 在setBlockOnDisconnect再執行scanForPeripherals():

    [weakBaby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
            NSLog(@"Peripheral Disconnected");      
            // We're disconnected, so start scanning again
            weakBaby.scanForPeripherals().begin();
        }];
    

    但在BabyCentralManager.m 裡面的didDiscoverPeripheral執行了connectTimer:

    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
        ..............
        //处理连接设备
        if (needConnectPeripheral) {
            if ([currChannel filterOnconnectToPeripherals](peripheral.name,advertisementData,RSSI)) {
                [centralManager connectPeripheral:peripheral options:[currChannel babyOptions].connectPeripheralWithOptions];
                //开一个定时器监控连接超时的情况
                connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(disconnect:) userInfo:peripheral repeats:NO];
            }
        }
    }
    //停止扫描
    - (void)disconnect:(id)sender {
        [centralManager stopScan];
    }
    

    scanForPeripherals().begin();會被強制停止

    opened by sjacs5537 8
  • 在swift中搜索不到设备??

    在swift中搜索不到设备??

        baby = BabyBluetooth.shareBabyBluetooth()
        babyDelegate()
        baby?.scanForPeripherals()
        baby?.begin()
    

    并且不能象例子中的 baby?.scanForPeripherals().begin()这样使用。 以上代码在控制台只显示了:

    CBCentralManagerStatePoweredOn 就没有了

    opened by miws 7
  • 扫描就连接,判断蓝牙名,结果连接多个设备

    扫描就连接,判断蓝牙名,结果连接多个设备

    1.次问题应该给iOS设备版本无关,我已经用iPad,iPhone测试过 2.主要实现功能是,扫描到名字XXX立即连接蓝牙,结果连接成功代理方法,连接了多个能扫描到的设备(是指 我连接硬件,结果,硬件,我手机,电脑,别人电脑都连接)

       //设置扫描到设备的委托
        [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
            NSLog(@"搜索到了设备:%@", peripheral.name);
            if ([peripheral.name isEqualToString:@"XXX"]) {
                //本次只执行一次,一开始我考虑到线程锁问题,但是后来,我改用全局变量存,依然有问题
                NSLog(@"连接设备222222222222222");
                currentPeripheral=peripheral;//全局变量蓝牙
                //我测试1秒以上延时可以,
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    //里面连接没有使用传过去的,而是使用全局变量的蓝牙
                    [weakSelf loadData:[peripheral copy]];
                });
            }
        }];
    

    可以看看我代码里面的注释和说明,如果需要,可以下载demo:https://share.weiyun.com/358c0f14893127a69b7089fb98bb9e97

    3.过滤代理,只过滤需要的那个蓝牙,也会出现问题,但是,不开硬件不会连接,过滤名字长度大于0,也出现问题,应该是连接蓝牙瞬间出现问题

    opened by fanxiangyang 5
  • 手机蓝牙写数据如何获取回调状态

    手机蓝牙写数据如何获取回调状态

    /** 写Characteristic成功后的block | when did write value for characteristic successed */

    • (void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block; 以上函数,回调空,而且ios10 报错
    opened by ggdkay 5
  • 关于蓝牙后台发送广播不广播Local Name和Service UUID‘s的问题

    关于蓝牙后台发送广播不广播Local Name和Service UUID‘s的问题

    首先,这是当前我遇到了一个蓝牙方面很是头疼的问题,并不是针对BabyBluetooth蓝牙库的,如果您知道的话,请给与一些提示和帮助。当前的流程是手机发送蓝牙广播,硬件设备扫描广播数据,使用广播数据里Local Name手动写入的唯一标示来匹配,之所以采取这种模式是因为,硬件和手机的关系是一对多,而蓝牙连接的话,苹果设备一联接,其他苹果设备就扫描不到了,只能一对一,所以没有使用蓝牙连接。但是这样的话会出现一个问题,就是苹果蓝牙后台广播的限制,原本发送广播时Advertisement Data会有两个段分别是Local Name和Service UUID‘s这两个字段,但如果在后台发送广播时,是不发送在这两个字段的。那么,在后台模式下,所能扫描的广播数据有哪些是可以进行自定义的呢?如果后台广播的问题不能解决,能否有其他方案来实现硬件和手机之间一对多的匹配,这个问题麻烦了,谢谢。

    question 
    opened by sontie 5
  • 重新扫描问题

    重新扫描问题

    您好,我最近在写项目中使用该框架遇到一个问题,我在连上某个蓝牙设备后将该设备设为自动重连.[_baby AutoReconnect:_currentPeripheral]; 然后在设备断开5秒后将它从自动重连中去除 [_baby AutoReconnectCancel:_currentPeripheral]; 并重新开启扫描,这时候我发现一旦这个蓝牙设备可以被连接会直接跳过条件过滤重新连接,并且不会去搜索蓝牙服务和特征. @coolnameismy 能帮我看一下是什么原因吗? 我的本意是想要在app连上某个设备后记住该设备,在该设备断开重连5秒没连上才去重新扫描连接别的设备.

    opened by Yangxiiii 4
  • 关于设置过滤条件

    关于设置过滤条件

    您好,目前我们公司的项目正在使用这个框架.确实方便很多,很好用.我的问题如下:希望得到回复,谢谢!

    在设置过滤条件的时候,我搜索指定 设备的信号,然后将搜索到的信号记录下来,但是当设备的信号消失的时候现在就不会返回true了,我也就没有办法记录了,具体截图上传了,我想要在 这个设备没有信号的时候也记录他.应该怎么处理呢?

    opened by ziyouzhe4 4
  • 本机已连接的蓝牙设备又被重新扫描到

    本机已连接的蓝牙设备又被重新扫描到

    大神,碰到一个问题,望相助: 是这样的,我用LightBlue先连接上我的蓝牙设备A,然后打开我自己的app之后还能扫描到A,并且这时候的A的state变成CBPeripheralStateDisconnected了,直接导致LightBlue断开了与A的连接。但是,我用BabyBluetooth的demo却没有这样的问题(不会扫描出本机其他app已经连接的蓝牙设备)。

    我的部分代码是这样的:

    self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    [self.manager scanForPeripheralsWithServices:nil options:nil];
    -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
        if (![self.nDevices containsObject:peripheral]) {
            [self.nDevices addObject:peripheral];
            [self.manager connectPeripheral:peripheral options:nil];
        }
    }
    

    看了BabyBluetooth的源码,也没发现有什么设置来避免扫描到其他app或自己的app已连接的蓝牙设备。请问大神有没有碰到这种问题,该怎么解决?

    opened by archerLj 3
  • Reading is not permitted

    Reading is not permitted

    我在调取babyBluetooth.channel(channelOnCharacteristicView).characteristicDetails(self.currPeripheral,self.characteristic)的时候一直报Reading is not permitted 我是蓝牙小白, 能告诉我为什么吗

    opened by vicky1221 3
  • 关于出现CBCentralManagerStateUnsupported的问题

    关于出现CBCentralManagerStateUnsupported的问题

    您好, 打印log: 2017-05-15 14:01:35.126650 BabyBluetoothAppDemo[1598:680455] (null) 2017-05-15 14:01:35.181769 BabyBluetoothAppDemo[1598:680455] viewDidLoad 2017-05-15 14:01:35.460185 BabyBluetoothAppDemo[1598:680455] viewDidAppear 2017-05-15 14:01:35.460501 BabyBluetoothAppDemo[1598:680455] >>>已切换到默认频道 2017-05-15 14:01:35.460875 BabyBluetoothAppDemo[1598:680455] >>> 第1次等待CBCentralManager打开 2017-05-15 14:01:35.469307 BabyBluetoothAppDemo[1598:680455] >>>CBCentralManagerStatePoweredOn 2017-05-15 14:01:35.475639 BabyBluetoothAppDemo[1598:680455] >>>CBCentralManagerStateUnsupported 2017-05-15 14:01:35.475898 BabyBluetoothAppDemo[1598:680455] >>>CBPeripheralManagerStatePoweredOn 2017-05-15 14:01:35.476196 BabyBluetoothAppDemo[1598:680455] >>>CBPeripheralManagerStatePoweredOn 2017-05-15 14:01:37.652845 BabyBluetoothAppDemo[1598:680497] >>> 第2次等待CBCentralManager打开 2017-05-15 14:01:39.848072 BabyBluetoothAppDemo[1598:680497] >>> 第3次等待CBCentralManager打开 2017-05-15 14:01:42.048046 BabyBluetoothAppDemo[1598:680497] >>> 第4次等待CBCentralManager打开 2017-05-15 14:01:44.214740 BabyBluetoothAppDemo[1598:680497] >>> 第5次等待CBCentralManager 打开任然失败,请检查你蓝牙使用权限或检查设备问题。

    出现这种情况的代码如下: baby = [BabyBluetooth shareBabyBluetooth]; babyLV = [[BabyBluetooth alloc] init];

    是不是说CBCentralManager只能创建一次?

    opened by ldezho 2
  • 数据发送

    数据发送

    我对蓝牙刚开始接触,我们公司想用手机于设备蓝牙通信,手机可以给设备发送数据,也可以接收数据,我用你的BabyBluetoothAppDemo分别安装在两台真机上演示,发现两个手机都能相互连接对方,然后我点击你的按钮“写(0x11)”写数据给对方,我在A机写后,在B机上为什么无法读到数据啊 ,B机完全没反应。请大神指点。

    opened by yuxingmin 2
  • Send commands regularly

    Send commands regularly

    每隔10ms发一次数据,发送命令到发送命令成功中间耗时约60ms,这个间隔时间能缩短吗? 是设备需要修改协议,还是其他的问题?

    Data is sent every 10ms, and it takes about 60ms from sending the command to sending the command successfully. Can this interval be shortened? Does the device need to modify the protocol, or is it another problem?

    opened by liuxz656 0
Releases(0.7.0)
  • 0.7.0(May 6, 2016)

    • setBlockOnReadValueForDescriptors 委托中的一个参数命名错误,把参数名 CBDescriptor *descriptorNSError 改为 CBDescriptor *descriptor
    • 一些方法细节修改,增加代码的兼容性和健壮性
    • 添加一个新的方法enjoy() ,相当于链式方法中的 connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); 简写,这个方法是一个语法糖,可以减少代码量。
    • 完善测试用例
    • 提取Babybluetooth中一些默认行为的常量,统一放在BabyDefine.h文件中
    • 修复findConnectPeripheral中的一个bug fixed #35
    • 添加一个开关,控制Babybluetooth中打印的日志是否开启 ,使用方式可以通过修改源码 BabyDefine.h #define BABY_IS_SHOW_LOG 1 0为不打印,1为打印日志
    • 修复一个当discoveryPeripheral block未设置时会导致discoveryPeripheralFilter不起作用的bug"
    • 添加蓝牙中心模式各种事件的委托,使用notification方式做广播,开发者现在不但可以使用block实现委托,也可以使用notification实现各种事件的委托监听,从而得到更为灵活的开发方式,通知类型:
    //centralManager status did change notification
    #define BabyNotificationAtCentralManagerDidUpdateState @"BabyNotificationAtCentralManagerDidUpdateState"
    //did discover peripheral notification
    #define BabyNotificationAtDidDiscoverPeripheral @"BabyNotificationAtDidDiscoverPeripheral"
    //did connection peripheral notification
    #define BabyNotificationAtDidConnectPeripheral @"BabyNotificationAtDidConnectPeripheral"
    //did filed connect peripheral notification
    #define BabyNotificationAtDidFailToConnectPeripheral @"BabyNotificationAtDidFailToConnectPeripheral"
    //did disconnect peripheral notification
    #define BabyNotificationAtDidDisconnectPeripheral @"BabyNotificationAtDidDisconnectPeripheral"
    //did discover service notification
    #define BabyNotificationAtDidDiscoverServices @"BabyNotificationAtDidDiscoverServices"
    //did discover characteristics notification
    #define BabyNotificationAtDidDiscoverCharacteristicsForService @"BabyNotificationAtDidDiscoverCharacteristicsForService"
    //did read or notify characteristic when received value  notification
    #define BabyNotificationAtDidUpdateValueForCharacteristic @"BabyNotificationAtDidUpdateValueForCharacteristic"
    //did write characteristic and response value notification
    #define BabyNotificationAtDidWriteValueForCharacteristic @"BabyNotificationAtDidWriteValueForCharacteristic"
    //did change characteristis notify status notification
    #define BabyNotificationAtDidUpdateNotificationStateForCharacteristic @"BabyNotificationAtDidUpdateNotificationStateForCharacteristic"
    //did read rssi and receiced value notification
    #define BabyNotificationAtDidReadRSSI @"BabyNotificationAtDidReadRSSI"
    
    //蓝牙扩展通知
    // did centralManager enable notification
    #define BabyNotificationAtCentralManagerEnable @"BabyNotificationAtCentralManagerEnable"
    
    • 添加了一个方法:根据外设UUID对应的string获取已配对的外设
    /**
     根据外设UUID对应的string获取已配对的外设
    
     通过方法获取外设后可以直接连接外设,跳过扫描过程
     */
    - (CBPeripheral *)retrievePeripheralWithUUIDString:(NSString *)UUIDString;
    

    一个工具方法,用来根据UUIDString获取外设,使用示例:

    //快速获取外设
    CBPeripheral *p = [baby retrievePeripheralWithUUIDString:@"B19A6ED7-29D5-67EF-0207-6F5AE8BC337B"];
    //直接根据外设连接
    baby.having(p).connectToPeripherals().begin();
    

    另外,如果不适用最新的api,也可以使用下面的方式达到同样的效果。

      NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B19A6ED7-29D5-67EF-0207-6F5AE8BC337B"];
       CBPeripheral *p = [self.baby.centralManager retrievePeripheralsWithIdentifiers:@[uuid]][0];
    baby.having(p).connectToPeripherals().begin();
    
    Source code(tar.gz)
    Source code(zip)
  • 0.6.0(Mar 17, 2016)

    • 修改了设置读取到Descriptors方法参数的bug
    • 修改了私有变量名 babyBluetooth.m中,私有变量babysiter改名为babyCentralManager
    • 根据代码规范优化了代码
    • 外设模式中,当未指定外设名称时,增加一个默认名称
    • 因为单词拼错导致修改了一个方法名: setFilterOnConnetToPeripherals 改成 setFilterOnConnectToPeripherals
    • 在BabyBluetooth.h 文件头几行添加了当前版本号
    • 在BabyBluetooth的.h文件中为每个类的一句话的作用描述
    • 逐步补充测试用例
    • baby添加缓存所有搜索到的外设索引,解决在使用过程中由于未保存peripheral对象可以导致的未知错
    • 添加2个方法,用户管理外设的自动重连功能
    /**
     添加断开自动重连的外设
     */
    - (void)AutoReconnect:(CBPeripheral *)peripheral;
    
    /**
     删除断开自动重连的外设
     */
    - (void)AutoReconnectCancel:(CBPeripheral *)peripheral;
    
    • 搜索和连接的过滤器方法中添加了rssi和advertisementData两个参数
    //v0.5.0
    /**
    设置查找Peripherals的规则
    |  filter of discover peripherals 
    */
    - (void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName))filter;
    /**
    设置连接Peripherals的规则
    |  setting filter of connect to peripherals  peripherals 
    */
    - (void)setFilterOnConnectToPeripherals:(BOOL (^)(NSString *peripheralName))filter;
    /**
    设置查找Peripherals的规则
    |  filter of discover peripherals 
    */
    - (void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel
                                          filter:(BOOL (^)(NSString *peripheralName))filter;
    /**
    设置连接Peripherals的规则
    |  setting filter of connect to peripherals  peripherals 
    */
    - (void)setFilterOnConnectToPeripheralsAtChannel:(NSString *)channel
                                         filter:(BOOL (^)(NSString *peripheralName))filter;
    
    
    //0.6.0
    /**
    设置查找Peripherals的规则
    |  filter of discover peripherals 
    */
    - (void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
    /**
    设置连接Peripherals的规则
    |  setting filter of connect to peripherals  peripherals 
    */
    - (void)setFilterOnConnectToPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
    /**
    设置查找Peripherals的规则
    |  filter of discover peripherals 
    */
    - (void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel
                                          filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
    /**
    设置连接Peripherals的规则
    |  setting filter of connect to peripherals  peripherals 
    */
    - (void)setFilterOnConnectToPeripheralsAtChannel:(NSString *)channel
                                         filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
    
    
    
    Source code(tar.gz)
    Source code(zip)
  • 0.5.0(Dec 16, 2015)

    • 修改了babybluetooth.h 方法的注释
    • 添加了babybluetooth对外设模式的支持,现在可以用babybluetooth实现蓝牙设备的模拟
    
        //配置第一个服务s1
        CBMutableService *s1 = makeCBService(@"FFF0");
        //配置s1的3个characteristic
        makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//读
        makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//写
        makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//读写,自动生成uuid
        makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段
        makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段
        //配置第一个服务s2
        CBMutableService *s2 = makeCBService(@"FFE0");
        makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读
        //实例化baby
        baby = [BabyBluetooth shareBabyBluetooth];
        //配置委托
        [self babyDelegate];
        //添加服务和启动外设
        baby.bePeripheral().addServices(@[s1,s2]).startAdvertising();
    
    
    • 添加block对peripheralManager主要委托的支持
    
    /**
     PeripheralManager did update state block
     */
    -(void)peripheralModelBlockOnPeripheralManagerDidUpdateState:(void(^)(CBPeripheralManager *peripheral))block;
    /**
     PeripheralManager did add service block
     */
    -(void)peripheralModelBlockOnDidAddService:(void(^)(CBPeripheralManager *peripheral,CBService *service,NSError *error))block;
    /**
     PeripheralManager did start advertising block
     */
    -(void)peripheralModelBlockOnDidStartAdvertising:(void(^)(CBPeripheralManager *peripheral,NSError *error))block;
    /**
     peripheralManager did receive read request block
     */
    -(void)peripheralModelBlockOnDidReceiveReadRequest:(void(^)(CBPeripheralManager *peripheral,CBATTRequest *request))block;
    /**
     peripheralManager did receive write request block
     */
    -(void)peripheralModelBlockOnDidReceiveWriteRequests:(void(^)(CBPeripheralManager *peripheral,NSArray *requests))block;
    /**
     peripheralManager did subscribe to characteristic block
     */
    -(void)peripheralModelBlockOnDidSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;
    /**
    peripheralManager did subscribe to characteristic block
    */
    -(void)peripheralModelBlockOnDidUnSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;
    
    
    • 添加了外设模式的使用示例程序 BabyBluetoothExamples/BluetoothStubOnIOS
    • 添加了生成UUID的方法 genUUID()
    • 修改了蓝牙中心模式实现类的类名,从babysiter改为babyCentralManager
    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Oct 26, 2015)

    0.4.0

    • 添加了一个demo ,可以作为蓝牙设备被发现、连接、读写、订阅
    • 项目支持cocoapods安装,并支持0.3.0 和 0.4.0两个版本
    • 增加了对获取RSSI的支持
    • 修改了wiki,对使用的每个方法增加了相关委托设置的说明
    • 修改了断开链接方法的委托
    //方法从 方法调用后的立刻回调 改为-》 方法执行后并且全部设备断开后的回调
    -(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;
    
    //删除了````-(void)cancelPeripheralConnection:(CBPeripheral *)peripheral;````方法调用后的委托
    -(void)setBlockOnCancelPeripheralConnectionBlock:(void(^)(CBCentralManager *centralManager,CBPeripheral *peripheral))block;
    -(void)setBlockOnCancelPeripheralConnectionBlockAtChannel:(NSString *)channel
                                                             block:(void(^)(CBCentralManager *centralManager,CBPeripheral *peripheral))block;
    //若想知道设备是否断开可以设置
    /*
    //断开Peripherals的连接的block
    -(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
    //或
    //断开Peripherals的连接的block
    -(void)setBlockOnDisconnectAtChannel:(NSString *)channel
                                   block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
    */
    
    
    • 添加了获取当前连接的peripheral的方法和获取centralManager的方法
    //获取当前连接的peripherals
    -(NSArray *)findConnectedPeripherals;
    //获取当前连接的peripheral
    -(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;
    //获取当前corebluetooth的centralManager对象
    -(CBCentralManager *)centralManager;
    
    • 增加了14个新的babybluetooth委托
    //写Characteristic成功后的block
    -(void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
    //写descriptor成功后的block
    -(void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block;
    //characteristic订阅状态改变的block
    -(void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
    //读取RSSI的委托
    -(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;
    //discoverIncludedServices的回调,暂时在babybluetooth中无作用
    -(void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block;
    //外设更新名字后的block
    -(void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block;
    //外设更新服务后的block
    -(void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
    
    /*channel*/
    //写Characteristic成功后的block
    -(void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel
                                                       block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
    //写descriptor成功后的block
    -(void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel
                                                   block:(void (^)(CBDescriptor *descriptor,NSError *error))block;
    //characteristic订阅状态改变的block
    -(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
                                                                    block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
    //读取RSSI的委托
    -(void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel
                                    block:(void (^)(NSNumber *RSSI,NSError *error))block;
    //discoverIncludedServices的回调,暂时在babybluetooth中无作用
    -(void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel
                                                              block:(void (^)(CBService *service,NSError *error))block;
    //外设更新名字后的block
    -(void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel 
                                      block:(void (^)(CBPeripheral *peripheral))block;
    //外设更新服务后的block
    -(void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel
                                          block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
    
    
    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Sep 30, 2015)

    0.3.0

    • add a new helper class :babyRhythm which can helper user to monitor Operation
    • ten new block about cancel sacn and disconnect peripheral and
    
    
        //when Peripherals FailToConnect
        -(void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
    
        //when disconnected Peripherals
        -(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
    
        //when Peripherals FailToConnect
        -(void)setBlockOnFailToConnectAtChannel:(NSString *)channel
                                          block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
        //when disconnected Peripherals
        -(void)setBlockOnDisconnectAtChannel:(NSString *)channel
                                       block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; 
    
    
        /#pragma mark -babybluetooth Special
    
        //after babyBluettooth cancelScan
        -(void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block;
        //after babyBluettooth cancelAllPeripheralsConnectionBlock
        -(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;
        //after babyBluettooth cancelPeripheralConnection
        -(void)setBlockOnCancelPeripheralConnectionBlock:(void(^)(CBCentralManager *centralManager,CBPeripheral *peripheral))block;
    
        //after babyBluettooth cancelScan方
        -(void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel
                                                 block:(void(^)(CBCentralManager *centralManager))block;
        //after babyBluettooth cancelAllPeripheralsConnectionBlock 
        -(void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel
                                                                     block:(void(^)(CBCentralManager *centralManager))block;
        //after babyBluettooth cancelPeripheralConnection 
        -(void)setBlockOnCancelPeripheralConnectionBlockAtChannel:(NSString *)channel
                                                                 block:(void(^)(CBCentralManager *centralManager,CBPeripheral *peripheral))block;
    
    • fix xcode7 waring
    • simplify readme,move some content to wiki
    • add runtime options for sacn,connect,discover service ...
    
    //set runtime option
    -(void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
                              connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
                            scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
                                      discoverWithServices:(NSArray *)discoverWithServices
                               discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
    
    //set runtime option at channel
    -(void)setBabyOptionsAtChannel:(NSString *)channel
     scanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
      connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
    scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
              discoverWithServices:(NSArray *)discoverWithServices
       discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
    
    

    exp:

    
        /*set babyOptions
            use in those function,if no option set nil
            - [centralManager scanForPeripheralsWithServices:scanForPeripheralsWithServices options:scanForPeripheralsWithOptions];
            - [centralManager connectPeripheral:peripheral options:connectPeripheralWithOptions];
            - [peripheral discoverServices:discoverWithServices];
            - [peripheral discoverCharacteristics:discoverWithCharacteristics forService:service];
    
            at channel function:
                [baby setBabyOptionsAtChannel:<#(NSString *)#> scanForPeripheralsWithOptions:<#(NSDictionary *)#> connectPeripheralWithOptions:<#(NSDictionary *)#> scanForPeripheralsWithServices:<#(NSArray *)#> discoverWithServices:<#(NSArray *)#> discoverWithCharacteristics:<#(NSArray *)#>]
         */
    
        //sacnfor options->CBCentralManagerScanOptionAllowDuplicatesKey:忽略同一个Peripheral端的多个发现事件被聚合成一个发现事件
        NSDictionary *scanForPeripheralsWithOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@YES};
        /*connect options-> only works in non-background model
         CBConnectPeripheralOptionNotifyOnConnectionKey 
         CBConnectPeripheralOptionNotifyOnDisconnectionKey 
         CBConnectPeripheralOptionNotifyOnNotificationKey
        */
         NSDictionary *connectOptions = @{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES,
         CBConnectPeripheralOptionNotifyOnDisconnectionKey:@YES,
         CBConnectPeripheralOptionNotifyOnNotificationKey:@YES};
    
        [baby setBabyOptionsAtChannel:channelOnPeropheralView scanForPeripheralsWithOptions:scanForPeripheralsWithOptions connectPeripheralWithOptions:connectOptions scanForPeripheralsWithServices:nil discoverWithServices:nil discoverWithCharacteristics:nil];
    
    
    Source code(tar.gz)
    Source code(zip)
Owner
刘彦玮
阿里云-iot事业部招前端,风光无限 一起造作,有兴趣的给我投简历[email protected]
刘彦玮
RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant.

RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant. It's backed by RxSwift and CoreBluetooth and it prov

Polidea 1.3k Jan 6, 2023
Diabetes: test the FreeStyle Libre glucose sensor as a Bluetooth Low Energy device, even directly from an Apple Watch.

Since the FreeStyle Libre 2 / 3 glucose sensors are Bluetooth Low Energy devices, I am trying to leverage their capabilities to implement something ne

Guido Soranzio 6 Jan 2, 2023
BluetoothKit Easily communicate between iOS devices using BLE.

BluetoothKit Easily communicate between iOS devices using BLE. Background Apple mostly did a great job with the CoreBluetooth API, but because it enca

Rasmus Høhndorf Hummelmose 2.1k Jan 8, 2023
MacOS app which allows drag and drop of images to BLE thermal printers

Print2BLE Copyright (c) 2021 BitBank Software, Inc. Written by Larry Bank [email protected] What is it? This project is a MacOS GUI applicatio

Larry Bank 37 Jan 5, 2023
This is a simple app, which scans for BLE Peripherials and connect to them. The example works with NORDIC_UART_SERVICE.

BLE Serial IOs Example This is a simple app, which scans for BLE Peripherials and connect to them. The example works with NORDIC_UART_SERVICE. UUIDS H

Muhammad Hammad 4 May 10, 2022
Swift implementation of Xiaomi's BLE authentication

MiAuth Xiaomi M365/Mi Authentication library written in Swift. Features M365 Authentication Mi Authentication (WIP - Login doesn't work reliably yet)

null 2 Jul 27, 2022
A very simple library to discover and retrieve data from nearby devices (even if the peer app works at background).

Discovery: A simple library to discover and retrieve data from nearby devices. Discovery is a very simple but useful library for discovering nearby de

Ömer Faruk Gül 412 Dec 19, 2022
iOS Bluetooth LE framework

Features A futures interface replacing protocol implementations. Timeout for Peripheral connection, Service scan, Service + Characteristic discovery a

Troy Stribling 696 Dec 25, 2022
Blocks Based Bluetooth LE Connectivity framework for iOS/watchOS/tvOS/OSX. Quickly configure centrals & peripherals, perform read/write operations, and respond characteristic updates.

ExtendaBLE Introduction ExtendaBLE provides a very flexible syntax for defining centrals and peripherals with ease. Following a blocks based builder a

Anton 94 Nov 29, 2022
Fluetooth - Flutter library for sending bytes to Bluetooth devices on Android/iOS

A Flutter library for sending bytes to Bluetooth devices. Available on Android a

Iandi Santulus 1 Jan 2, 2022
The Bluetooth LE library for iOS and Mac. 100% Swift.

iOS-BLE-Library An in-development Bluetooth Low Energy Library by Nordic Semiconductor to interact with the , which is not complicated, but requires w

Nordic Semiconductor 6 Dec 19, 2022
Bluejay is a simple Swift framework for building reliable Bluetooth LE apps.

Bluejay is a simple Swift framework for building reliable Bluetooth LE apps. Bluejay's primary goals are: Simplify talking to a single Bluetooth LE pe

Steamclock Software 1k Dec 13, 2022
Omnipod Bluetooth PumpManager For Loop

OmniBLE Omnipod Bluetooth PumpManager For Loop Status This module is at the very beginning stages of development and does not even compile yet. DO NOT

Randall Knutson 20 Apr 21, 2022
Build your own 'AirTags' 🏷 today! Framework for tracking personal Bluetooth devices via Apple's massive Find My network.

OpenHaystack is a framework for tracking personal Bluetooth devices via Apple's massive Find My network.

Secure Mobile Networking Lab 5.8k Jan 9, 2023
Simple, block-based, lightweight library over CoreBluetooth. Will clean up your Core Bluetooth related code.

LGBluetooth Simple, block-based, lightweight library over CoreBluetooth. Steps to start using Drag and Drop it into your project Import "LGBluetooth.h

null 170 Sep 19, 2022
Bluetooth mapping in Swift

Bluetonium is part of the E-sites iOS Suite. Bluetonium is a Swift Library that makes it easy to communicate with Bluetooth devices. Features ?? Servi

E-sites 165 Nov 20, 2022
Bluetooth mesh messaging SDK for apps

Berkanan SDK Berkanan SDK enables Bluetooth mesh messaging between nearby apps. It's the framework used by Berkanan Messenger (Product Hunt, TechCrunc

Zsombor Szabo 189 Jan 1, 2023
MiniVendingMachine - SwiftUI demo Apple Watch app to open a mini vending machine via bluetooth

Mini Vending Machine Use Apple Watch to open vending machine cells. Note: This a

CGH 3 Apr 8, 2022
AZPeerToPeerConnectivity is a wrapper on top of Apple iOS Multipeer Connectivity framework. It provides an easier way to create and manage sessions. Easy to integrate

AZPeerToPeerConnection Controller Features Multipeer Connectivity Connection via Bluetooth or Wifi No need write all session, browser, services delega

Afroz Zaheer 66 Dec 19, 2022