蓝牙实现流程:
1.建立中心管理者
2.扫描外设
3.连接外设
4.扫描外设的服务和特征
5.与外设进行数据交互
代码实现:
第一:建立中心管理者
首先:导入#import <CoreBluetooth/CoreBluetooth.h>框架
初始化CBCentralManager对象并设置代理以及队列,queue:nil,默认就是在主线程
<CBCentralManagerDelegate>
@property (nonatomic, strong) CBCentralManager *centralManager;
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
接着调用CBCentralManagerDelegate必须实现的方法- (void)centralManagerDidUpdateState:(CBCentralManager *)central
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@"CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
NSLog(@"CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
NSLog(@"CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@"CBCentralManagerStatePoweredOff");
break;
case CBCentralManagerStatePoweredOn:
NSLog(@"CBCentralManagerStatePoweredOn");
break;
default:
break;
}
}
第二:扫描外设
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
//扫描外设,第一个参数:nil,表示扫描所有外设,也可以根据CBUUID数组查找指定外设
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
} else {
NSLog(@"请打开蓝牙");
}
}
扫描到外设之后,会调用此方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
//连接外设
}
第三:连接外设
@property (nonatomic, strong) CBPeripheral *peripheral;
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"找到蓝牙外设 名称:%@",peripheral.name);
if ([peripheral.name isEqualToString:@"要连接的蓝牙名称"]) {
//这里要注意一点:peripheral在该方法里不被持有,调用结束后会被自动摧毁,保存外设,使其被持有
self.peripheral = peripheral;
//连接外设
[self.centralManager connectPeripheral:peripheral options:nil];
} else {
NSLog(@"未找到要连接的外设");
}
}
connectPeripheral方法后会执行以下代理方法
//外设连接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"连接蓝牙成功");
//设置代理以及查找服务
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
//外设连接失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"连接失败");
}
//断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"断开连接");
}
第四:扫描外设的服务和特征
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"连接蓝牙成功");
//设置代理<CBPeripheralDelegate>以及查找服务
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
#pragma mark- CBPeripheralDelegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
NSLog(@"error: %@",error);
return;
}
for (CBService *service in peripheral.services) {
//扫描服务中的特征
[peripheral discoverCharacteristics:nil forService:service];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristics in service.characteristics) {
if ([characteristics.UUID isEqual:[CBUUID UUIDWithString:@"writeUUID"]]) {
//向外设发送数据:data
[peripheral writeValue:[NSData data] forCharacteristic:characteristics type:CBCharacteristicWriteWithResponse];
} else if ([characteristics.UUID isEqual:[CBUUID UUIDWithString:@"readUUID"]]) {
[peripheral readValueForCharacteristic:characteristics];
} else if ([characteristics.UUID isEqual:[CBUUID UUIDWithString:@"notifyUUID"]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristics];
}
}
}
第五:与外设进行数据交互
[self.peripheral writeValue:[NSData data] forCharacteristic:characteristics type:CBCharacteristicWriteWithResponse];
//获取characteristic的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"收到数据:%@",characteristic.value);
//此处对外设返回的数据进行处理
}
网友评论