You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

552 lines
17 KiB

/*
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