BabyBluetooth 是1个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx。
最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。1整条链下来要近10几个拜托方法,并且不断的在拜托方法中调用方法再进入其他的拜托,致使代码很零散。因此我就想让coreBluetooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。
更新于:20150916,现在BabyBluetooth 已有了96个star
更新于:20160129,现在BabyBluetooth 已有了880个star
//导入.h文件和系统蓝牙库的头文件 #import "BabyBluetooth.h" -(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 setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { NSLog(@"装备:%@--连接成功",peripheral.name); }]; //设置发现装备的Services的拜托 [baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) { for (CBService *service in peripheral.services) { NSLog(@"搜索到服务:%@",service.UUID.UUIDString); } }]; //设置发现设service的Characteristics的拜托 [baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { NSLog(@"===service name:%@",service.UUID); for (CBCharacteristic *c in service.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 *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); }]; //过滤器 //设置查找装备的过滤器 [baby setDiscoverPeripheralsFilter:^BOOL(NSString *peripheralsFilter) { //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1 if (peripheralsFilter.length >1) { return YES; } return NO; }]; //设置连接的装备的过滤器 __block BOOL isFirst = YES; [baby setFilterOnConnetToPeripherals:^BOOL(NSString *peripheralName) { //这里的规则是:连接第1个AAA打头的装备 if(isFirst && [peripheralName hasPrefix:@"AAA"]){ isFirst = NO; return YES; } return NO; }]; }
CoreBluetooch中实现上扫描,连接,发现服务和characteristic和它的值相干方法调用是很麻烦啰嗦混乱的。以下:centralManager启动->状态拜托->调用扫描方法->进入扫描到装备的拜托->调用连接装备方法->进入连接到装备的拜托->发现服务方法->发现服务拜托->发现characteristic方法->发现characteristic拜托->读characteristic的value->读characteristic的value的拜托->读description,读description的value-> ….的拜托
而BabyBluetooth只需要1句话就履行了上面的内容。
//扫描装备 然后读取服务,然后读取characteristics名称和值和属性,获得characteristics对应的description的名称和值 baby.scanForPeripherals().connectToPeripheral().discoverServices() .discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic() .readValueForDescriptors().begin();
另外一方面,BabyBluetooth所有的拜托方法都紧凑的聚在了1起。另外,快速示例中没有包括channel的使用,如果包括了channel,那末ios几个页面或组件的蓝牙调用模块都可以写在1起,看起来就觉得很方便。
关于更多BabyBluetooth的介绍和使用示例已api,请移步到BabyBluetooth主页查看
上一篇 web前端开发笔记