Skip to content
liuyanwei edited this page Sep 29, 2015 · 8 revisions

QuickExample

//import header file
#import "BabyBluetooth.h"

-(void)viewDidLoad {
    [super viewDidLoad];

    //get babyBluetooth instance
    baby = [BabyBluetooth shareBabyBluetooth];
    //setting delegate
    [self babyDelegate];
    //just use it,no need wait CBCentralManagerStatePoweredOn
    baby.scanForPeripherals().begin();
}

//setting delegate
-(void)babyDelegate{

    //delegate of DiscoverToPeripherals
    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"discovered peripheral:%@",peripheral.name);
    }];
   
    //Filter
    //setting filter of discoverPeripheral
    [baby setDiscoverPeripheralsFilter:^BOOL(NSString *peripheralsFilter) {
        //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
        if (peripheralsFilter.length >1) {
            return YES;
        }
        return NO;
    }];
    

}
  

init

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

scanfor

    //scan for peripheral   
    baby.scanForPeripherals().begin();
    
    //scan for ten seconds and stop (disconnect peripheral and cancel scan for)
    baby.scanForPeripherals().begin().stop(10);
    
    //setting filter of discoverPeripheral
    [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) {
        //设置查找规则是名称大于1 , the search rule is peripheral.name length > 2
        if (peripheralName.length >2) {
            return YES;
        }
        return NO;
    }];

scan for then connect

    
    /*
    *scan for then connect:1:setting filter 2:scan and connect
    */
    //1:setting filter
     __block BOOL isFirst = YES;
    [baby setFilterOnConnetToPeripherals:^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()

connect direct

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

disconnect and cancel

  
  //disconnect peripheral,peripheral is a instance of CBPeripheral
  [baby cancelPeripheralConnection:peripheral];
  
  //disconnect all peripheral
  [baby cancelAllPeripheralsConnection];
    
  //cancelScan
  [baby cancelScan];
  

services_characteristic_description

discover 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();
   

services_characteristic_description

  //self.peripheral and self.characteristic is instance
  baby.characteristicDetails(self.peripheral,self.characteristic);

subscript of characteristic

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

unsubscript of characteristic

  //self.peripheral and self.characteristic is instance
  [baby cancelNotify:self.peripheral characteristic:self.characteristic];