CoreBluetooth的实现代码

//
//  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值