Skip to content
liuyanwei edited this page Oct 7, 2015 · 8 revisions

QuickExample

//导入.h文件和系统蓝牙库的头文件
#import "BabyBluetooth.h"
//定义变量
BabyBluetooth *baby;

-(void)viewDidLoad {
    [super viewDidLoad];

    //初始化BabyBluetooth 蓝牙库
    baby = [BabyBluetooth shareBabyBluetooth];
    //设置蓝牙委托
    [self babyDelegate];
    //设置委托后直接可以使用,无需等待CBCentralManagerStatePoweredOn状态
    baby.scanForPeripherals().begin();
}

//设置蓝牙委托
-(void)babyDelegate{

    //设置扫描到设备的委托
    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"搜索到了设备:%@",peripheral.name);
    }];
   
    //过滤器
    //设置查找设备的过滤器
    [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) {
        //设置查找规则是名称大于1 , the search rule is 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];

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