1.本地集成BabyBluetooth
2.pod集成
pod 'BabyBluetooth'
3.代码
HGBluetoothManager.h
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface HGBluetoothManager : NSObject
+(id)sharedInstance;
@property(assign)BOOL isBluetoohConnect;
//开始蓝牙请求
-(void)startBluetooth;
@property (nonatomic,copy) void(^onBluetoothConnect)(BOOL bluetoothConnect);
//获取CBCentralManager
-(CBCentralManager*)getCentralManager;
//关闭蓝牙请求
-(void)stopBluetooth;
//退出单例
-(void)Delloc;
@end
HGBluetoothManager.m
#import "HGBluetoothManager.h"
#import "BabyBluetooth.h"
#define WEAK_SELF __weak __typeof(self) weakSelf = self
#define STRONG_SELF __strong __typeof(weakSelf) self = weakSelf
@interface HGBluetoothManager (){
BOOL begainConnect;
}
//蓝牙管理
@property(nonatomic,retain)BabyBluetooth *baby;
//当前设备
@property(nonatomic,retain)CBPeripheral *currentPeripheral;
//当前特征
@property(nonatomic,retain)CBCharacteristic *currentCharacteristic;
@end
@implementation HGBluetoothManager
static HGBluetoothManager *hgBluetoothManager = nil;
static dispatch_once_t onceToken;
+(id)sharedInstance
{
dispatch_once(&onceToken, ^{
hgBluetoothManager = [[self alloc] init];
});
return hgBluetoothManager;
}
-(void)initBaby{
if(!self.baby){
//初始化BabyBluetooth 蓝牙库
self.baby = [BabyBluetooth shareBabyBluetooth];
}
}
-(CBCentralManager*)getCentralManager{
return self.baby.centralManager;
}
//开始蓝牙请求
-(void)startBluetooth{
[self initBaby];
//停止之前的连接
[self.baby cancelAllPeripheralsConnection];
//设置委托后直接可以使用,无需等待CBCentralManagerStatePoweredOn状态。
self.baby.scanForPeripherals().begin();
[self babyDelegate];
begainConnect=false;
}
//蓝牙网关初始化和委托方法设置
-(void)babyDelegate{
WEAK_SELF;
[self.baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
STRONG_SELF;
if (@available(iOS 10.0, *)) {
if (central.state == CBManagerStatePoweredOn) {
//停止之前的连接
[self.baby cancelAllPeripheralsConnection];
self.baby.scanForPeripherals().begin();
}else if (@available(iOS 10.0, *)) {
if (central.state == CBManagerStatePoweredOff){
}
} else {
// Fallback on earlier versions
}
} else {
// Fallback on earlier versions
}
}];
//设置设备连接成功委托
[self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) {
STRONG_SELF;
NSLog(@"设备:%@--连接成功",peripheral.name);
[self.baby cancelScan];
if(self.onBluetoothConnect){
self.onBluetoothConnect(true);
}
}];
//设置设备连接失败委托
[self.baby setBlockOnFailToConnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
STRONG_SELF;
NSLog(@"设备:%@--连接失败",peripheral.name);
self->begainConnect=false;
self.baby.scanForPeripherals().begin();
if(self.onBluetoothConnect){
self.onBluetoothConnect(false);
}
}];
//设置设备断开连接委托
[self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
STRONG_SELF;
NSLog(@"设备:%@--断开连接",peripheral.name);
self->begainConnect=false;
//停止之前的连接
[self.baby cancelAllPeripheralsConnection];
if(self.onBluetoothConnect){
self.onBluetoothConnect(false);
}
}];
//设置扫描到设备的委托
[self.baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
STRONG_SELF;
if ([peripheral.name hasPrefix:@"XXXXX"]&&!self->begainConnect) {
self->begainConnect=true;
//开始连接设备
self.currentPeripheral=peripheral;
self.baby.having(peripheral).connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
}
}];
//设置发现设service的Characteristics的委托
[self.baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
STRONG_SELF;
// NSLog(@"===service name:%@",service.UUID);
for (CBCharacteristic *c in service.characteristics) {
// NSLog(@"====charateristic name is :%@",c.UUID);
NSString *characteristicName=[NSString stringWithFormat:@"%@",c.UUID];
if([characteristicName hasPrefix:@"FFF1"]){
[self.baby notify:weakSelf.currentPeripheral
characteristic:c
block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
//接收到值会进入这个方法
if([c.value isEqual:[NSNull null]]||c.value==NULL){
return ;
}
NSData *bluetoothData=c.value;
Byte *dataByte=(Byte *)[bluetoothData bytes];
}];
}else if([characteristicName hasPrefix:@"FFF2"]){
self.currentCharacteristic=c;
}
}
}];
//设置读取characteristics的委托
[self.baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value);
}];
//设置发现characteristics的descriptors的委托
[self.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的委托
[self.baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) {
NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value);
}];
//设置查找设备的过滤器
[self.baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {
//最常用的场景是查找某一个前缀开头的设备
if ([peripheralName hasPrefix:@"XXXXX"]) {
return YES;
}
return NO;
}];
//设置连接设备的过滤器
[self.baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {
if ([peripheralName hasPrefix:@"XXXXX"]) {
return YES;
}
return NO;
}];
[self.baby setBlockOnCancelAllPeripheralsConnectionBlock:^(CBCentralManager *centralManager) {
NSLog(@"setBlockOnCancelAllPeripheralsConnectionBlock");
}];
[self.baby setBlockOnCancelScanBlock:^(CBCentralManager *centralManager) {
NSLog(@"setBlockOnCancelScanBlock");
}];
[self.baby setBlockOnDidWriteValueForCharacteristic:^(CBCharacteristic *characteristic, NSError *error) {
NSLog(@"DidWriteValueForCharacteristic");
}];
//示例:
//扫描选项->CBCentralManagerScanOptionAllowDuplicatesKey:忽略同一个Peripheral端的多个发现事件被聚合成一个发现事件
NSDictionary *scanForPeripheralsWithOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@NO};
//连接设备->
[self.baby setBabyOptionsWithScanForPeripheralsWithOptions:scanForPeripheralsWithOptions connectPeripheralWithOptions:nil scanForPeripheralsWithServices:nil discoverWithServices:nil discoverWithCharacteristics:nil];
}
-(void)stopBluetooth{
[self.baby cancelScan];
[self.baby cancelAllPeripheralsConnection];
self.baby=nil;
}
//退出单例
-(void)Delloc{
[self stopBluetooth];
hgBluetoothManager=nil;
onceToken=0;
}
@end
网友评论