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

baby的使用分为2步。设置block委托调用方法

本章节只说明中心模式的使用,外设模式请见 [外设模式]

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, NSDictionary *advertisementData, NSNumber *RSSI) {
        //最常用的场景是查找某一个前缀开头的设备
        //if ([peripheralName hasPrefix:@"Pxxxx"] ) {
        //    return YES;
        //}
        //return NO;
        //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
        if (peripheralName.length >1) {
            return YES;
        }
        return NO;
    }];
}
  

初始化

    //单例初始化 推荐
    BabyBluetooth *baby = [BabyBluetooth shareBabyBluetooth];
    //常规初始化
    BabyBluetooth *baby = [[BabyBluetooth alloc]init];

相关委托设置

//设备状态改变的block
-(void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block;
-(void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel
                                                 block:(void (^)(CBCentralManager *central))block;

//示例:
[baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
    if (central.state == CBCentralManagerStatePoweredOn) {
        [SVProgressHUD showInfoWithStatus:@"设备打开成功,开始扫描设备"];
    }
}];

搜索设备

    //搜索设备   
    baby.scanForPeripherals().begin();
    
    //搜索设备10秒后停止   
    baby.scanForPeripherals().begin().stop(10);

相关委托设置

//找到Peripherals的block
-(void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;

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

//示例:
//设置查找设备的过滤器
[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;
    //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
    if (peripheralName.length >1) {
        return YES;
    }
    return NO;
}];

 



//设置扫描到设备的委托
[baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
    NSLog(@"搜索到了设备:%@",peripheral.name);
    [weakSelf insertTableView:peripheral advertisementData:advertisementData];
}];

搜索并连接设备

    
    /*
    *搜索设备后连接设备:1:先设置连接的设备的filter 2进行连接
    */
    //1:设置连接的设备的过滤器
     __block BOOL isFirst = YES;
    [baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName) {
        //这里的规则是:连接第一个AAA打头的设备
        if(isFirst && [peripheralName hasPrefix:@"AAA"]){
            isFirst = NO;
            return YES;
        }
        return NO;
    }];

    //2 扫描、连接
    baby.scanForPeripherals().connectToPeripherals().begin()

相关委托设置

//连接Peripherals成功的block
-(void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;
//连接Peripherals失败的block
-(void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
//断开Peripherals的连接的block
-(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//连接Peripherals成功的block
-(void)setBlockOnConnectedAtChannel:(NSString *)channel
                              block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;

//连接Peripherals失败的block
-(void)setBlockOnFailToConnectAtChannel:(NSString *)channel
                                  block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//断开Peripherals的连接的block
-(void)setBlockOnDisconnectAtChannel:(NSString *)channel
                               block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//示例:

//设置设备连接成功的委托,同一个baby对象,使用不同的channel切换委托回调
[baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
    NSLog(@"设备:%@--连接成功",peripheral.name);
}];

//设置设备连接失败的委托
[baby setBlockOnFailToConnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
    NSLog(@"设备:%@--连接失败",peripheral.name);
}];

//设置设备断开连接的委托
[baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
    NSLog(@"设备:%@--断开连接",peripheral.name);
}];

直接连接设备

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

断开连接和取消扫描

  
  //断开单个peripheral的连接
  [baby cancelPeripheralConnection:peripheral];//peripheral是一个CBPeripheral的实例
  
  //断开所有peripheral的连
  [baby cancelAllPeripheralsConnection];
    
  //取消扫描
  [baby cancelScan];
  

相关委托设置

//断开Peripherals的连接的block
-(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
-(void)setBlockOnDisconnectAtChannel:(NSString *)channel
                               block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;

//cancelScan方法调用后的回调
-(void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block;
//cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
-(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;

//cancelScan方法调用后的回调
-(void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel
                                         block:(void(^)(CBCentralManager *centralManager))block;
//cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
-(void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel
                                                             block:(void(^)(CBCentralManager *centralManager))block;

services_characteristic_description

获取设备的services、characteristic、description以及value

  //设置peripheral 然后读取services,然后读取characteristics名称和值和属性,获取characteristics对应的description的名称和值
  //self.peripheral是一个CBPeripheral实例
   baby.having(self.peripheral).connectToPeripherals().discoverServices().discoverCharacteristics()
   .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
   

相关委托设置

//设置查找服务的block
-(void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block;
//设置查找到Characteristics的block
-(void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;
//设置获取到最新Characteristics值的block
-(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
//设置查找到Descriptors名称的block
-(void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
//设置读取到Descriptors值的block
-(void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block;
or AtChannel 版本

//示例:

//设置发现设备的Services的委托
[baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) {
    for (CBService *s in peripheral.services) {
        //每个service
    }
}];
//设置发现设service的Characteristics的委托
[baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
    NSLog(@"===service name:%@",service.UUID);
}];
//设置读取characteristics的委托
[baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
    NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value);
}];
//设置发现characteristics的descriptors的委托
[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);
    }
}];
//设置读取Descriptor的委托
[baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) {
    NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value);
}];

获取一个characteristic的value和全部description及description的value

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

相关委托设置

//委托示例同上。该方法会读取characteristic的全部value和description及value

通知方式监听一个characteristic的值

      //self.peripheral是一个CBPeripheral实例,self.characteristic是一个CBCharacteristic实例
            [baby notify:self.currPeripheral
          characteristic:self.characteristic
                   block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
                 //接收到值会进入这个方法
                NSLog(@"new value %@",characteristics.value);
     }];

相关委托设置

//设置获取到最新Characteristics值的block
-(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
-(void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel
                                               block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;

//characteristic订阅状态改变的block
-(void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
//characteristic订阅状态改变的block
-(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
                                                                block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

取消通知

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

相关委托设置

//设置获取到最新Characteristics值的block
-(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
//characteristic订阅状态改变的block
-(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
                                                                block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

获取centralManager和当前连接的peripheral

//获取centralManager
-(CBCentralManager *)centralManager;
//获取当前连接的peripherals
-(NSArray *)findConnectedPeripherals;
//获取当前连接的peripheral
-(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;

读取rssi

  1. 未连接状态读取RSSI,在setBlockOnDiscoverToPeripherals委托中有RSSI数据
//读取RSSI的委托
-(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;
//示例
[baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
        NSLog(@"RSSI:%@",RSSI);
    }];
  1. 连接状态读取RSSI:调用CBPeripheral的readRSSI方法,读取成功后进入相关委托

相关委托

//读取RSSI的委托
-(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;
-(void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel
                                block:(void (^)(NSNumber *RSSI,NSError *error))block;
    

写characteristic和写description

直接调用原生peripheral的方法writeValue forCharacteristic或writeValue forDescriptor

//相关委托

//写Characteristic成功后的block
-(void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
-(void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel
                                                   block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;

//写descriptor成功后的block
-(void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block;
-(void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel
                                               block:(void (^)(CBDescriptor *descriptor,NSError *error))block;


//示例:写一0X01到characteristic
Byte b = 0X01;
NSData *data = [NSData dataWithBytes:&b length:sizeof(b)];
[self.currPeripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
//若最后一个参数是CBCharacteristicWriteWithResponse,则会进入setBlockOnDidWriteValueForCharacteristic委托

外设断开自动重连

我们常常因为一些未知原因导致设备断开连接后,比如因为距离远,设备断电,设备不稳定等原因断开连接后,需要重新连接才能正常使用。重新连接的方法很简单,只需要在设备断开后的委托中,重新调用一下连接外设的方法即可使连接条件回复后重新连接外设。

示例:

    //断开连接的委托
    [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
        NSLog(@"设备:%@--断开连接",peripheral.name);
       [weakSelf.baby.centralManager connectPeripheral:peripheral options:nil];
    }];
 //如果原生委托则在这个委托中执行
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
}

baby中对重连更简单的支持

baby最新版本中增加了对断线重连的支持

    //设置重新连接的设备
     [baby AutoReconnect:peripheral];

通过设置一个自动重连对象,当这个peripheral断开后,会在baby内部的centralManager自动重新连接它。

当然,你也可以调用 [baby AutoReconnectCancel:peripheral];方法取消自动重连对象

更多参考

Clone this wiki locally