蓝牙开发
想要进行蓝牙开发,需要添加系统的CoreBluetooth框架CoreBluetooth.framework
#import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate> //外设管理者 @property (nonatomic, strong) CBCentralManager * manager; //存放所有扫描到的外设 @property (nonatomic, strong) NSMutableArray * peripheralArrayM; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建中央管理者 self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; //扫描周边的外设 [self.manager scanForPeripheralsWithServices:nil options:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.manager stopScan]; } #pragma mark - CBCentralManagerDelegate //实时更新扫描到的设备状态 - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSLog(@"------%ld", (long)central.state); } /** 1.发现外围设备 @param central central @param peripheral 外围设备 @param advertisementData 相关信息 @param RSSI 信号强度 */ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI { if (![self.peripheralArrayM containsObject:peripheral]) { [self.peripheralArrayM addObject:peripheral]; } //2.连接对应的外设 [self.manager connectPeripheral:peripheral options:nil]; peripheral.delegate = self; } //3.中心管理者连接外围设备成功之后会调用 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { //4.扫描服务 (nil:会发现所有服务) [peripheral discoverServices:nil]; } #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { //5.扫描到服务 NSArray * services = peripheral.services; for (CBService * service in services) { //每一个服务,根据服务去扫描 if ([service.UUID.UUIDString isEqualToString:@"你想要操作的服务"]) { //6.让外设去发现该服务的所有特征 [peripheral discoverCharacteristics:nil forService:service]; } } } /** 外设发现特征 @param peripheral 外设 @param service 服务 @param error error */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error { //7.获取到对应服务的特征 NSArray * characteristics = service.characteristics; for (CBCharacteristic * characteristic in characteristics) { //每一个特征 if ([characteristic.UUID.UUIDString isEqualToString:@"你想要操作的特性"]) { //8.向对应的特征写、读数据 [peripheral writeValue:[[NSData alloc] init] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; [peripheral readValueForCharacteristic:characteristic]; } } } - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(nonnull CBDescriptor *)descriptor error:(nullable NSError *)error { //9.向对应的特征写数据 NSLog(@"---9.向对应的特征写数据---%@", descriptor.UUID.UUIDString); } - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error { //10.对应的特征读数据 NSLog(@"---10.对应的特征读数据---%@", descriptor.UUID.UUIDString); } #pragma mark - 懒加载 - (NSMutableArray *)peripheralArrayM { if (_peripheralArrayM == nil) { _peripheralArrayM = [NSMutableArray array]; } return _peripheralArrayM; } @end
更多内容--> 博客导航 每周一篇哟!!!
有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!
程序猿 CoderEYLee https://github.com/lieryang