#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interfaceViewController()
/** 系统蓝牙管理类*/
@property(nonatomic,strong) CBCentralManager *bleCentral;
/** 找到的指定的设备*/
@property (nonatomic, strong) CBPeripheral *findPeripheral;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
// 创建蓝牙操作线程
dispatch_queue_t queue = dispatch_queue_create("gjw.ble.thread", DISPATCH_QUEUE_CONCURRENT);
// 创建系统蓝牙管理类
_bleCentral = [[CBCentralManager alloc] initWithDelegate:selfqueue:queue];
}
#pragma mark - CBCentralManagerDelegate
/** 蓝牙连接状态改变*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
HDDEBUGLOG(@"蓝牙状态改变 %ld",central.state);
// 如果蓝牙不是开启状态
if(central.state != CBManagerStatePoweredOn){ return; }
// 自定义的服务ID。注意这里是服务ID,不是设备的[peripheral.identifier UUIDString]这个值
CBUUID* serviceId = [CBUUIDUUIDWithString:@"自定义的服务ID"];
// 根据自定义的服务ID找出手机系统已连接的设备
NSArray* peripheralArray = [_bleCentral retrieveConnectedPeripheralsWithServices:@[serviceId]];
// 如果没有发现已经连接的设备
if(peripheralArray.count==0){
HDDEBUGLOG(@"没有发现已连接的设备");
// -------------------- 扫描设备 -------------------
// 设置 不重复扫描已发现设备
NSDictionary *option = @{ CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO] };
// 开始扫描
[_bleCentral scanForPeripheralsWithServices:niloptions:option];
}else{
// 遍历系统已连接的设备
for(CBPeripheral* peripheralinperipheralArray) {
NSString* deviceId = [peripheral.identifierUUIDString];
// 这里必须保存这个引用才可以连接设备成功,不然会连接失败
self.findPeripheral = peripheral;
HDDEBUGLOG(@"存在设备 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",peripheral.name, peripheral.state, deviceId);
[_bleCentral connectPeripheral:peripheral options:nil];
}
}
}
/**
* 发现外设
* advertisementData 是广播的值,一般携带设备名,serviceUUIDs等信息
* RSSI绝对值越大,表示信号越差,设备离的越远。如果想装换成百分比强度,(RSSI+100)/100
*/
- (void)centralManager:(CBCentralManager*)centraldidDiscoverPeripheral:(CBPeripheral*)peripheraladvertisementData:(NSDictionaryid> *)advertisementDataRSSI:(NSNumber*)RSSI{
// 设备id
NSString* deviceId = [peripheral.identifierUUIDString];
// 设备名称
NSString* deviceName = peripheral.name;
// 根据设备名找到指定设备
if([deviceNameisEqualToString:@"自定义的设备名"]){
HDDEBUGLOG(@"发现设备 - peripheral.name: %@ RSSI: %@ 设备ID: %@",peripheral.name, RSSI, deviceId);
// 这里必须保存这个引用才可以连接设备成功,不然会连接失败
self.findPeripheral = peripheral;
[_bleCentral connectPeripheral:peripheral options:nil];
}
}
/** 连接外设成功*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
// 设备id
NSString* deviceId = [peripheral.identifierUUIDString];
NSString* deviceName = peripheral.name;
HDDEBUGLOG(@"连接设备成功 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",deviceName, peripheral.state, deviceId);
// 注意这里是服务ID,不是设备的[peripheral.identifier UUIDString]这个值
CBUUID* serviceId = [CBUUIDUUIDWithString:@"自定义的服务ID"];
NSArray* peripheralArray = [_bleCentral retrieveConnectedPeripheralsWithServices:@[serviceId]];
for(CBPeripheral* peripheralinperipheralArray) {
HDDEBUGLOG(@"------已连接的设备 - peripheral.name: %@ 连接状态:%ld 设备ID: %@ ",peripheral.name, peripheral.state, deviceId);
}
}
/** 连接外设失败*/
- (void)centralManager:(CBCentralManager*)centraldidFailToConnectPeripheral:(CBPeripheral*)peripheralerror:(nullableNSError*)error{}
/** 已经断开连接*/
- (void)centralManager:(CBCentralManager*)centraldidDisconnectPeripheral:(CBPeripheral*)peripheralerror:(nullableNSError*)error{}
#pragma mark - CBPeripheralDelegate
/** 发现服务*/
- (void)peripheral:(CBPeripheral*)peripheraldidDiscoverServices:(NSError*)error{}
/** 发现特征*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{}
/** 订阅的特征值的value发生改变的回调*/
// read 外设 --> 中心 (由控制类去解析返回的数据)
- (void)peripheral:(CBPeripheral*)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic*)characteristicerror:(NSError*)error{}
/**
* 当写入某个特征值后 外设代理执行的回调
* 发送类型必须是CBCharacteristicWriteWithResponse才会回调
*/
- (void)peripheral:(CBPeripheral*)peripheraldidWriteValueForCharacteristic:(CBCharacteristic*)characteristicerror:(NSError*)error{}
/**
* 读取信号强度回调的方法
* peripheral 调用 readRSSI方法触发该代理
*/
-(void)peripheral:(CBPeripheral*)peripheraldidReadRSSI:(NSNumber*)RSSIerror:(NSError*)error{}
@end
::仅做笔记
网友评论