蓝牙库
#import "BabyBluetooth.h"
-(void)viewDidLoad {
[superviewDidLoad];
//初始化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 setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
NSLog(@"设备:%@--连接成功",peripheral.name);
}];
//设置发现设备的Services的委托
[baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) {
for(CBService *serviceinperipheral.services) {
NSLog(@"搜索到服务:%@",service.UUID.UUIDString);
}
}];
//设置发现设service的Characteristics的委托
[baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
NSLog(@"===service name:%@",service.UUID);
for(CBCharacteristic *cinservice.characteristics) {
NSLog(@"charateristic name is :%@",c.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 *dincharacteristic.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);
}];
//过滤器
//设置查找设备的过滤器
[baby setDiscoverPeripheralsFilter:^BOOL(NSString *peripheralsFilter) {
//设置查找规则是名称大于1 , the search rule is peripheral.name length > 1
if(peripheralsFilter.length >1) {
returnYES;
}
returnNO;
}];
//设置连接的设备的过滤器
__block BOOL isFirst = YES;
[baby setFilterOnConnetToPeripherals:^BOOL(NSString *peripheralName) {
//这里的规则是:连接第一个AAA打头的设备
if(isFirst && [peripheralName hasPrefix:@"AAA"]){
isFirst = NO;
returnYES;
}
returnNO;
}];
}