#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
typedef void (^BluetoothStateUpdateBlock)(CBCentralManager *central);
typedef void (^DiscoverPeripheralBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI);
@interface IBeaconManager : NSObject<CBCentralManagerDelegate>
{
/** 蓝牙状态*/
BluetoothStateUpdateBlock btStateUpdateBlock;
/** 发现一个蓝牙外设的回调 */
DiscoverPeripheralBlock btDiscoverPeripheralBlcok;
}
+(instancetype)share;
/**系统当前蓝牙的状态
* @param stateBlock 实时返回当前蓝牙状态
*/
- (void)returnBluetoothStateWithBlock:(BluetoothStateUpdateBlock)stateBlock;
/**开始搜索蓝牙外设,每次在block中返回一个蓝牙外设信息
* @param nameStr 模糊搜索设备 目标设备名称包含字段(该参数视需求而定)
* 返回的block参数可参考CBCentralManager 的 centralManager:didDiscoverPeripheral:advertisementData:RSSI:
* @param discoverBlock 搜索到蓝牙外设后的回调
*/
- (void)scanForPeripheralsWithPrefixNameString:(NSString *)nameStr
discoverPeripheral:(DiscoverPeripheralBlock)discoverBlock;
/**
* 停止扫描
*/
- (void)stopScan;
@end
#import "IBeaconManager.h"
@implementation IBeaconManager
{
CBCentralManager *btCentralManager;
// 模糊搜索参数
NSString * btNameString;
}
static IBeaconManager *shareManager = nil;
+(instancetype)share{
static dispatch_once_t oneToken;
dispatch_once(&oneToken, ^{
shareManager = [[IBeaconManager alloc]init];
});
return shareManager;
}
- (instancetype)init
{
self = [super init];
if (self) {
btCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
return self;
}
- (void)dealloc
{
btCentralManager.delegate = nil;
btCentralManager = nil;
}
#pragma mark -
//主设备状态改变
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
if(btStateUpdateBlock){
btStateUpdateBlock(central);
}
}
//扫描到设备
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSString * name = @"";
for(NSString *key in [advertisementData allKeys]){
if([key isEqual:@"kCBAdvDataLocalName"]){
name = advertisementData[key];
}
}
if([name containsString:btNameString] && btDiscoverPeripheralBlcok){
btDiscoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
}else if([name containsString:@"JDY"] && btDiscoverPeripheralBlcok){
btDiscoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
}
}
#pragma mark -
/**系统当前蓝牙的状态*/
- (void)returnBluetoothStateWithBlock:(BluetoothStateUpdateBlock)stateBlock{
btStateUpdateBlock = stateBlock;
btCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/**开始搜索蓝牙外设,每次在block中返回一个蓝牙外设信息*/
- (void)scanForPeripheralsWithPrefixNameString:(NSString *)nameStr
discoverPeripheral:(DiscoverPeripheralBlock)discoverBlock{
btNameString = nameStr;
btDiscoverPeripheralBlcok = discoverBlock;
[btCentralManager scanForPeripheralsWithServices:nil options:nil];
}
// 停止扫描
- (void)stopScan{
[btCentralManager stopScan];
}
#pragma mark -
/**data转换为十六进制的string*/
- (NSString *)hexStringFromData:(NSData *)myD{
Byte *bytes = (Byte *)[myD bytes];
//下面是Byte 转换为16进制。
NSString *hexStr=@"";
for(int i=0;i<[myD length];i++){
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
if([newHexStr length]==1){
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
}else{
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
}
return hexStr;
}
@end