Skip to content
coolnameismy edited this page Mar 16, 2016 · 8 revisions

babyBluetooth usage has two step, set babybluetooth delegateand using method

QuickExample

//import header
#import "BabyBluetooth.h"
//declaration baby 
BabyBluetooth *baby;

-(void)viewDidLoad {
    [super viewDidLoad];

    //BabyBluetooth init
    baby = [BabyBluetooth shareBabyBluetooth];
    //set babybluetooth delegate
    [self babyDelegate];
    //direct use,no longer wait for status Of CBCentralManagerStatePoweredOn
    baby.scanForPeripherals().begin();
}

//set babybluetooth delegate
-(void)babyDelegate{

    //when scanfor perihpheral
    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"find peripheral:%@",peripheral.name);
    }];
   

    //filter
    //discover peripherals filter
    [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;
        //rule is periperal.name.length larger then 1
        peripheral.name length > 1
        if (peripheralName.length >1) {
            return YES;
        }
        return NO;
    }];
}
  

init

    //singleton ,recommend
    BabyBluetooth *baby = [BabyBluetooth shareBabyBluetooth];
    //normal
    BabyBluetooth *baby = [[BabyBluetooth alloc]init];

reference

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

//example:
[baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
    if (central.state == CBCentralManagerStatePoweredOn) {
        [SVProgressHUD showInfoWithStatus:@"central open succeed"];
    }
}];

scanfor

    //scanfor peripheral  
    baby.scanForPeripherals().begin();
    
    //scanfor peripheral ,after 10 secend will stop()
    baby.scanForPeripherals().begin().stop(10);

referance

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

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

//example:
//filter of discover peripherals
[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;
    //rule is periperal.name.length larger then 1
    //设置查找规则是名称大于1 , the search rule is peripheral.name length > 2
    if (peripheralName.length >2) {
        return YES;
    }
    return NO;
}];

//when scanfor peripheral  
[baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
    NSLog(@"did find peripheral:%@",peripheral.name);
    [weakSelf insertTableView:peripheral advertisementData:advertisementData];
}];

scan for then connect

    
    /*
    *scan for then connect:1:setting filter 2:scan and connect
    */
    //1:setting filter
     __block BOOL isFirst = YES;
    [baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName) {
        //rule:first peripheral and name start with "AAA"
        if(isFirst && [peripheralName hasPrefix:@"AAA"]){
            isFirst = NO;
            return YES;
        }
        return NO;
    }];

    //2:scan and connect
    baby.scanForPeripherals().connectToPeripherals().begin()

reference

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

//example:

//when did connected to peripheral 
[baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
    NSLog(@"peripheral:%@--connect succeed",peripheral.name);
}];

//when fail to connect peripheral
[baby setBlockOnFailToConnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
    NSLog(@"peripheral:%@--connect fail",peripheral.name);
}];

//when disconnected peripheral
[baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
    NSLog(@"peripheral:%@--disconnect succeed",peripheral.name);
}];

connect direct

 baby.having(self.currPeripheral).connectToPeripherals().begin();

disconnect and cancel

  
  //disconnect a peripheral
  [baby cancelPeripheralConnection:peripheral];//peripheral是一个CBPeripheral的实例
  
  //disconnect all peripheral
  [baby cancelAllPeripheralsConnection];
    
  //cancel scanfor peripheral
  [baby cancelScan];
  

reference

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

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

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

services_characteristic_description

get services、characteristic、description and those value

  /set a peripheral then discoverServices,and then characteristics and its value,then characteristics’s description name and value
  //self.peripheral is a CBPeripheral instance
   baby.having(self.peripheral).connectToPeripherals().discoverServices().discoverCharacteristics()
   .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
   

reference

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




//example:

//when discover services of peripheral
[baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) {
    for (CBService *s in peripheral.services) {
        //do something
    }
}];

//when discovered Characteristics
[baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
    NSLog(@"===service name:%@",service.UUID);
}];

//when read new characteristics value  or notiy a characteristics value
[baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
    NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value);
}];

//when discover descriptors for characteristic
[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);
    }
}];

//when read descriptors for characteristic
[baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) {
    NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value);
}];

read the characteristic's services, characteristic,description and those value

  //self.peripheral是一个CBPeripheral实例,self.characteristic是一个CBCharacteristic实例
  baby.characteristicDetails(self.peripheral,self.characteristic);

reference

//as some as before

subscript of characteristic

    
         [baby notify:self.currPeripheral
          characteristic:self.characteristic
                   block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
                 //when did receive data
                NSLog(@"new value %@",characteristics.value);
     }];

reference

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

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

unsubscript of characteristic

  //self.peripheral是一个CBPeripheral实例,self.characteristic是一个CBCharacteristic实例
  [baby cancelNotify:self.peripheral characteristic:self.characteristic];

reference

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

fetch centralManager and connected peripherals

//fetch centralManager
-(CBCentralManager *)centralManager;
//fetch peripherals on connected
-(NSArray *)findConnectedPeripherals;
//fetch peripheral on connected by peripheral.name
-(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;

read rssi

  1. unconnected scenario

if peripheral unconnected,you can read RSSI in block:setBlockOnDiscoverToPeripherals

example

//when discover peripheral
[baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"RSSI:%@",RSSI);
    }];
  1. connected scenario

if peripheral did connected ,we can use [peripheral readRSSi] for read RSSI,then go into block : setBlockOnDidReadRSSI or setBlockOnDidReadRSSIAtChannel

reference

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

write characteristic or write description

ues [peripheral writeValue forCharacteristic] or [writeValue forDescriptor] method

reference

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

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

//example
//:write 一0X01 to characteristic
Byte b = 0X01;
NSData *data = [NSData dataWithBytes:&b length:sizeof(b)];
[self.currPeripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
//if last paramter CBCharacteristicWriteWithResponse decide whether into  setBlockOnDidWriteValueForCharacteristic delegate

re-connect for peripheral

sometimes we want reconnect peripheral when it disconnect, you can use centralManager connectPeripheral methord on delegate of didDisconnectPeripheral

example:

    //断开连接的委托
    [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
        NSLog(@"设备:%@--断开连接",peripheral.name);
       [weakSelf.baby.centralManager connectPeripheral:peripheral options:nil];
    }];
     //core bluetooth delegate
    - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    }

baby re-connect

newest baby support reconnect for perpheral

     [baby AutoReconnect:peripheral];

set a peripheral object , if this disconnect ,and it will auto reconnect

you also can use [baby AutoReconnectCancel:peripheral]; to cancel it!

Clone this wiki locally