Skip to content
coolnameismy edited this page Oct 31, 2015 · 4 revisions

babyBluetooth usage has two step, set babybluetooth delegateand using method

example

    //setting  delegate of discoverToPeripherals
    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"has scan for peropheral:%@",peripheral.name);
    }];
    //setting delegate of peripheral connected
    [baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
        NSLog(@"peripheral:%@connected",peripheral.name);
    }];
    

after setting delegate ,then execute serial

baby.scanForPeripherals().begin();

when scan for a peripheral,or connected peripheral,it's into delegate on

all delegate

/* 
    default channel
 */

//when CentralManager state changed
-(void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block;

//when find peripheral
-(void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;

//when connected peripheral
-(void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;

//when fail to connect peripheral
-(void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//when disconnected peripheral
-(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//when discover services of peripheral
-(void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block;

//when discovered Characteristics
-(void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;

//when read new characteristics value  or notiy a characteristics value
-(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;

//when discover descriptors for characteristic
-(void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;

//when read descriptors for characteristic
-(void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block;

//when did write value for characteristic successed
-(void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

//when did write value for descriptor successed
-(void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block

//when characteristic notification state changed
-(void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

//when did read RSSI
-(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;

//no used in babybluetooth
-(void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block;

//when peripheral update name
-(void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block;

//when peripheral update services
-(void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;



/*
 channel delegate
 */

//when CentralManager state changed
-(void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel
                                                 block:(void (^)(CBCentralManager *central))block;

//when find peripheral
-(void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel
                                          block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;


//when connected peripheral
-(void)setBlockOnConnectedAtChannel:(NSString *)channel
                              block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;


//when fail to connect peripheral
-(void)setBlockOnFailToConnectAtChannel:(NSString *)channel
                                  block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//when disconnected peripheral
-(void)setBlockOnDisconnectAtChannel:(NSString *)channel
                               block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;


//when discover services of peripheral
-(void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel
                                     block:(void (^)(CBPeripheral *peripheral,NSError *error))block;

//when discovered Characteristics
-(void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel
                                            block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;

//when read new characteristics value  or notiy a characteristics value
-(void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel
                                               block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;

//when discover descriptors for characteristic
-(void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel
                                                         block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block;

//when read descriptors for characteristic
-(void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel
                                            block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block;


//when did write value for characteristic successed
-(void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel
                                                   block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

//when did write value for descriptor successed
-(void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel
                                               block:(void (^)(CBDescriptor *descriptor,NSError *error))block;


//when characteristic notification state changed
-(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
                                                                block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

//when did read RSSI
-(void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel
                                block:(void (^)(NSNumber *RSSI,NSError *error))block;



//when peripheral update name
-(void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel
                                  block:(void (^)(CBPeripheral *peripheral))block;

//when peripheral update services
-(void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel
                                      block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;


#pragma mark -babybluetooth filter

//filter of discover peripherals
-(void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName))filter;

//setting filter of connect to peripherals  peripherals
-(void)setFilterOnConnetToPeripherals:(BOOL (^)(NSString *peripheralName))filter;


//filter of discover peripherals
-(void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel
                                      filter:(BOOL (^)(NSString *peripheralName))filter;

//setting filter of connect to peripherals  peripherals
-(void)setFilterOnConnetToPeripheralsAtChannel:(NSString *)channel
                                     filter:(BOOL (^)(NSString *peripheralName))filter;


#pragma mark -babybluetooth Special

//when after call cancelScan
-(void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block;

//babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
//when did all peripheral disConnect
-(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;

//when after call cancelScan
-(void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel
                                         block:(void(^)(CBCentralManager *centralManager))block;

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

delegate at channe

delegate can switch by channel

usually in our app,more then one viewController want using bluetooth,you can setting delegate in defferent channel by group of setBlockOn...AtChannel method。and use baby.channel(channelName) to switch then.

example:

    //setting channel at “detailsView” when discover to peripheral
    NSString *channelName = @"detailsChannel";
    [baby setBlockOnDiscoverToPeripheralsAtChannel:channelName
          block:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
         NSLog(@"channel(%@) :搜索到了设备:%@",channelName,peripheral.name);
    }];
    
    //switch to channel 
    baby.channel(channelName)
    //execute
    .scanForPeripherals().begin();
    

##default channel in serial method of baby.().().(), channel() can be everywhere but after begin(),if serial without channel(), the channel in default channel.

Clone this wiki locally