CoreBluetooth的实现代码

来源:互联网 发布:淘宝装修助手 编辑:程序博客网 时间:2024/05/19 17:58
//
//  ViewController.m
//  CoreBluetooth
//
//  Created by 陈强 on 15/10/23.
//  Copyright © 2015年 com.chenqiang. All rights reserved.
//

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (nonatomic ,strong)CBCentralManager  *manager;
;
@property (nonatomic ,strong)NSMutableArray *peripherals;

@property (nonatomic ,strong)CBCharacteristic  *dataInteracther;

@property (nonatomic ,strong)CBCharacteristic  *datainterperipheral;





@end

@implementation ViewController

- (NSMutableArray *)peripherals
{
    if (!_peripherals) {
        self.peripherals = [NSMutableArray array];
        
    }
    return self.peripherals;
    
}

- (CBCentralManager *)manager
{
    //建立中心设备 用来管理中心设备,
    if (!_manager) {
        self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
        
    }
    return _manager;
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.建立中心设备
    self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    
    //扫描外设
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
    [self.manager scanForPeripheralsWithServices:@[@"123",@"345"] options:nil];
    

}

/**
 *  建立连接
 */


- (void)buildConnect
{
    for (CBPeripheral *peripheral in self.peripherals) {
        [self.manager connectPeripheral:peripheral options:nil];
        
    }
}



#warning mark -- CBcentralManagerdelegate

//发现外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
    //添加外围设备
    if (![self.peripherals containsObject:peripheral]) {
        
        //设置外设的代理
        peripheral.delegate = self;
        [self.peripherals addObject:peripheral];
        
    }
    
    
    

}

//连接到某个外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    //查找外设中所有服务
    [peripheral discoverServices:nil];
    
    
}

/**
 *  跟某个外接设备失去连接
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    
    
    
}


#warning  mark --- 外设的代理方法
/**
 *  外设已经找到服务
 */

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    //遍历所有的服务
    for (CBService *service in peripheral.services) {
        //过滤不要的服务
        if ([service.UUID isEqual:@"外设UUID"]) {
            //扫描服务下面的特征
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
        [peripheral discoverCharacteristics:@[@"123",@"345"]  forService:service];
        }
    }
    
}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    //遍历所有的特征
    for (CBCharacteristic *characteristic in service.characteristics) {
        //过滤不要的特征
        if ([characteristic.UUID isEqual:@"外设UUID"]) {
            //找到想要的特征
            self.dataInteracther = characteristic;
        }else if ([characteristic.UUID isEqual:@"789"]){
            self.datainterperipheral = characteristic;
            
        }
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 0
原创粉丝点击