藍(lán)牙實現(xiàn)流程:
1.建立中心管理者
2.掃描外設(shè)
3.連接外設(shè)
4.掃描外設(shè)的服務(wù)和特征
5.與外設(shè)進(jìn)行數(shù)據(jù)交互
代碼實現(xiàn):
第一:建立中心管理者
首先:導(dǎo)入#import <CoreBluetooth/CoreBluetooth.h>框架
初始化CBCentralManager對象并設(shè)置代理以及隊列掘猿,queue:nil素征,默認(rèn)就是在主線程
<CBCentralManagerDelegate>
@property (nonatomic, strong) CBCentralManager *centralManager;
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
接著調(diào)用CBCentralManagerDelegate必須實現(xiàn)的方法- (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;
}
}
第二:掃描外設(shè)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state == CBCentralManagerStatePoweredOn) {
//掃描外設(shè),第一個參數(shù):nil州泊,表示掃描所有外設(shè)励背,也可以根據(jù)CBUUID數(shù)組查找指定外設(shè)
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
} else {
NSLog(@"請打開藍(lán)牙");
}
}
掃描到外設(shè)之后,會調(diào)用此方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
//連接外設(shè)
}
第三:連接外設(shè)
@property (nonatomic, strong) CBPeripheral *peripheral;
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"找到藍(lán)牙外設(shè) 名稱:%@",peripheral.name);
if ([peripheral.name isEqualToString:@"要連接的藍(lán)牙名稱"]) {
//這里要注意一點:peripheral在該方法里不被持有斗搞,調(diào)用結(jié)束后會被自動摧毀指攒,保存外設(shè),使其被持有
self.peripheral = peripheral;
//連接外設(shè)
[self.centralManager connectPeripheral:peripheral options:nil];
} else {
NSLog(@"未找到要連接的外設(shè)");
}
}
connectPeripheral方法后會執(zhí)行以下代理方法
//外設(shè)連接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"連接藍(lán)牙成功");
//設(shè)置代理以及查找服務(wù)
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
//外設(shè)連接失敗
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"連接失敗");
}
//斷開連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"斷開連接");
}
第四:掃描外設(shè)的服務(wù)和特征
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"連接藍(lán)牙成功");
//設(shè)置代理<CBPeripheralDelegate>以及查找服務(wù)
[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) {
//掃描服務(wù)中的特征
[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"]]) {
//向外設(shè)發(fā)送數(shù)據(jù):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];
}
}
}
第五:與外設(shè)進(jìn)行數(shù)據(jù)交互
[self.peripheral writeValue:[NSData data] forCharacteristic:characteristics type:CBCharacteristicWriteWithResponse];
//獲取characteristic的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"收到數(shù)據(jù):%@",characteristic.value);
//此處對外設(shè)返回的數(shù)據(jù)進(jìn)行處理
}