123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- /*
- BabyBluetooth
- 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮
- https://github.com/coolnameismy/BabyBluetooth
-
- version:0.7.0
- */
- // Created by 刘彦玮 on 15/3/31.
- // Copyright (c) 2015年 刘彦玮. All rights reserved.
- #import <Foundation/Foundation.h>
- #import <CoreBluetooth/CoreBluetooth.h>
- #import "BabyCentralManager.h"
- #import "BabyPeripheralManager.h"
- #import "BabyToy.h"
- #import "BabySpeaker.h"
- #import "BabyRhythm.h"
- #import "BabyDefine.h"
- @interface BabyBluetooth : NSObject
- #pragma mark - babybluetooth的委托
- //默认频道的委托
- /**
- 设备状态改变的block | when CentralManager state changed
- */
- - (void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block;
- /**
- 找到Peripherals的block | when find peripheral
- */
- - (void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;
- /**
- 连接Peripherals成功的block
- | when connected peripheral
- */
- - (void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;
- /**
- 连接Peripherals失败的block
- | when fail to connect peripheral
- */
- - (void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
- /**
- 断开Peripherals的连接的block
- | when disconnected peripheral
- */
- - (void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
- /**
- 设置查找服务的block
- | when discover services of peripheral
- */
- - (void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block;
- /**
- 设置查找到Characteristics的block
- | when discovered Characteristics
- */
- - (void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;
- /**
- 设置获取到最新Characteristics值的block
- | when read new characteristics value or notiy a characteristics value
- */
- - (void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
- /**
- 设置查找到Descriptors名称的block
- | when discover descriptors for characteristic
- */
- - (void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
- /**
- 设置读取到Descriptors值的block
- | when read descriptors for characteristic
- */
- - (void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block;
- /**
- 写Characteristic成功后的block
- | when did write value for characteristic successed
- */
- - (void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
- /**
- 写descriptor成功后的block
- | when did write value for descriptor successed
- */
- - (void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block;
- /**
- characteristic订阅状态改变的block
- | when characteristic notification state changed
- */
- - (void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
- /**
- 读取RSSI的委托
- | when did read RSSI
- */
- - (void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;
- /**
- discoverIncludedServices的回调,暂时在babybluetooth中无作用
- | no used in babybluetooth
- */
- - (void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block;
- /**
- 外设更新名字后的block
- | when peripheral update name
- */
- - (void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block;
- /**
- 外设更新服务后的block
- | when peripheral update services
- */
- - (void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
- // channel的委托
- /**
- 设备状态改变的block
- | when CentralManager state changed
- */
- - (void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel
- block:(void (^)(CBCentralManager *central))block;
- /**
- 找到Peripherals的block
- | when find peripheral
- */
- - (void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel
- block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;
- /**
- 连接Peripherals成功的block
- | when connected peripheral
- */
- - (void)setBlockOnConnectedAtChannel:(NSString *)channel
- block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;
- /**
- 连接Peripherals失败的block
- | when fail to connect peripheral
- */
- - (void)setBlockOnFailToConnectAtChannel:(NSString *)channel
- block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
- /**
- 断开Peripherals的连接的block
- | when disconnected peripheral
- */
- - (void)setBlockOnDisconnectAtChannel:(NSString *)channel
- block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
- /**
- 设置查找服务的block
- | when discover services of peripheral
- */
- - (void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,NSError *error))block;
- /**
- 设置查找到Characteristics的block
- | when discovered Characteristics
- */
- - (void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;
- /**
- 设置获取到最新Characteristics值的block
- | when read new characteristics value or notiy a characteristics value
- */
- - (void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
- /**
- 设置查找到Characteristics描述的block
- | when discover descriptors for characteristic
- */
- - (void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block;
- /**
- 设置读取到Characteristics描述的值的block
- | when read descriptors for characteristic
- */
- - (void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block;
- /**
- 写Characteristic成功后的block
- | when did write value for characteristic successed
- */
- - (void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel
- block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
- /**
- 写descriptor成功后的block
- | when did write value for descriptor successed
- */
- - (void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel
- block:(void (^)(CBDescriptor *descriptor,NSError *error))block;
- /**
- characteristic订阅状态改变的block
- | when characteristic notification state changed
- */
- - (void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
- block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
- /**
- 读取RSSI的委托
- | when did read RSSI
- */
- - (void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel
- block:(void (^)(NSNumber *RSSI,NSError *error))block;
- /**
- discoverIncludedServices的回调,暂时在babybluetooth中无作用
- | no used in babybluetooth
- */
- - (void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel
- block:(void (^)(CBService *service,NSError *error))block;
- /**
- 外设更新名字后的block
- | when peripheral update name
- */
- - (void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral))block;
- /**
- 外设更新服务后的block
- | when peripheral update services
- */
- - (void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel
- block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
- #pragma mark - babybluetooth filter
- /**
- 设置查找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;
- #pragma mark - babybluetooth Special
- /**
- babyBluettooth cancelScan方法调用后的回调
- | when after call cancelScan
- */
- - (void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block;
- /**
- babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
- | when did all peripheral disConnect
- */
- - (void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;
- /**
- babyBluettooth cancelScan方法调用后的回调
- | when after call cancelScan
- */
- - (void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel
- block:(void(^)(CBCentralManager *centralManager))block;
- /**
- babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
- | when did all peripheral disConnect
- */
- - (void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel
- block:(void(^)(CBCentralManager *centralManager))block;
- /**
- 设置蓝牙运行时的参数
- | set ble runtime parameters
- */
- - (void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
- connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
- scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
- discoverWithServices:(NSArray *)discoverWithServices
- discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
- /**
- 设置蓝牙运行时的参数
- | set ble runtime parameters
- */
- - (void)setBabyOptionsAtChannel:(NSString *)channel
- scanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
- connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
- scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
- discoverWithServices:(NSArray *)discoverWithServices
- discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
- #pragma mark - 链式函数
- /**
- 查找Peripherals
- */
- - (BabyBluetooth *(^)()) scanForPeripherals;
- /**
- 连接Peripherals
- */
- - (BabyBluetooth *(^)()) connectToPeripherals;
- /**
- 发现Services
- */
- - (BabyBluetooth *(^)()) discoverServices;
- /**
- 获取Characteristics
- */
- - (BabyBluetooth *(^)()) discoverCharacteristics;
- /**
- 更新Characteristics的值
- */
- - (BabyBluetooth *(^)()) readValueForCharacteristic;
- /**
- 获取Characteristics的名称
- */
- - (BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic;
- /**
- 获取Descriptors的值
- */
- - (BabyBluetooth *(^)()) readValueForDescriptors;
- /**
- 开始执行
- */
- - (BabyBluetooth *(^)()) begin;
- /**
- sec秒后停止
- */
- - (BabyBluetooth *(^)(int sec)) stop;
- /**
- 持有对象
- */
- - (BabyBluetooth *(^)(id obj)) having;
- /**
- 切换委托的频道
- */
- - (BabyBluetooth *(^)(NSString *channel)) channel;
- /**
- 谓词,返回self
- */
- - (BabyBluetooth *) and;
- /**
- 谓词,返回self
- */
- - (BabyBluetooth *) then;
- /**
- 谓词,返回self
- */
- - (BabyBluetooth *) with;
- /**
- * enjoy 祝你使用愉快,
- *
- * 说明:enjoy是蓝牙全套串行方法的简写(发现服务,发现特征,读取特征,读取特征描述),前面可以必须有scanForPeripherals或having方法,channel可以选择添加。
-
- enjoy() 等同于 connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
-
- 它可以让你少敲很多代码
-
- ## 例子:
- - 扫描后来个全套(发现服务,发现特征,读取特征,读取特征描述)
-
- ` baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics()
- .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
- `
-
- - 直接使用已有的外设连接后全套(发现服务,发现特征,读取特征,读取特征描述)
- ` baby.having(self.peripheral).connectToPeripherals().discoverServices().discoverCharacteristics()
- .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
- `
- enjoy后面也可以加stop()方法
-
- */
- - (BabyBluetooth *(^)()) enjoy;
- #pragma mark - 工具方法
- /**
- * 单例构造方法
- * @return BabyBluetooth共享实例
- */
- + (instancetype)shareBabyBluetooth;
- /**
- 断开连接
- */
- - (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
- /**
- 断开所有连接
- */
- - (void)cancelAllPeripheralsConnection;
- /**
- 停止扫描
- */
- - (void)cancelScan;
- /**
- 更新Characteristics的值
- */
- - (BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails;
- /**
- 设置characteristic的notify
- */
- - (void)notify:(CBPeripheral *)peripheral
- characteristic:(CBCharacteristic *)characteristic
- block:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block;
- /**
- 取消characteristic的notify
- */
- - (void)cancelNotify:(CBPeripheral *)peripheral
- characteristic:(CBCharacteristic *)characteristic;
- /**
- 获取当前连接的peripherals
- */
- - (NSArray *)findConnectedPeripherals;
- /**
- 获取当前连接的peripheral
- */
- - (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;
- /**
- 获取当前corebluetooth的centralManager对象
- */
- - (CBCentralManager *)centralManager;
- /**
- 添加断开自动重连的外设
- */
- - (void)AutoReconnect:(CBPeripheral *)peripheral;
- /**
- 删除断开自动重连的外设
- */
- - (void)AutoReconnectCancel:(CBPeripheral *)peripheral;
- /**
- 根据外设UUID对应的string获取已配对的外设
-
- 通过方法获取外设后可以直接连接外设,跳过扫描过程
- */
- - (CBPeripheral *)retrievePeripheralWithUUIDString:(NSString *)UUIDString;
- #pragma mark - peripheral model
- //进入外设模式
- - (BabyPeripheralManager *(^)()) bePeripheral;
- - (BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName;
- @property (nonatomic, readonly) CBPeripheralManager *peripheralManager;
- //peripheral model block
- /**
- 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;
- @end
|