core Bluetooth(蓝牙4.0)

蓝牙4.0以低功耗著称,一般也叫BLE(Bluetooth Low Energy)。

目前主要应用的场景有:智能家居、运动手环和室内导航等。

利用core Bluetooth框架可以实现苹果设备与第三方蓝牙设备进行数据的交互。在CoreBluetooth框架中,有两个主要的角色:周边和中央 ,整个框架都是围绕这两个主要角色设计的,他俩之间有一系列的回调交换数据。core Bluetooth的核心框架图如下:

其中左边是中心,其中CBService 类代表服务,CBCharacteristic 类代表特征。右边是周边,其中CBMutableService 类代表服务,CBMutableCharacteristic 类代表特征。

注意一点就是:设备中包含服务,服务中包含特征。我们可以根据服务的唯一标示(UUID)找出设备中的服务,然后根据特征的唯一标示(UIID)从相应的服务中找出对应的特征,然后执行相应的操作。(就像中国包含省份,省份中有包含城市,我们可以根据省份的名称(类似于服务的UUID)来找到对应的省份,根据城市名称(类似于特征的UUID)从对应省份中找出正确的城市)。

core Bluetooth的主要开发步骤:

1. 建立中心设备,并成为自己的代理;
//1创建中心管理者
CBCentralManager *mgr = [[CBCentralManager alloc] init];
//成为自己的代理
mgr.delegate = self;
2. 使用中心设备扫描外设,并将扫描到的外设保存;
//2利用中心设备扫描(数组指定)外部设备(若数组为空,表示扫描所有的)
 [mgr scanForPeripheralsWithServices:(NSArray *) options:nil];
3. 根据外设的唯一标示,从保存的外设中连接外设;
//3根据外设的标示,连接外设
[self.mgr connectPeripheral:(CBPeripheral *) options:nil];
4. 如果连接外设成功,则扫描外设中的服务;
/** 连接外设成功调用 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    // 扫描外设中得服务
    [peripheral discoverServices:nil];
}

/**  连接外设失败调用 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{}
5. 实现外设的代理,如果找到正确的服务,则扫描服务中的特征;
/** 只要扫描到服务就会调用 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    // 获取外设中所有扫描到得服务
    NSArray *services = peripheral.services;
  遍历服务:
if (正确的服务){ // 从需要的服务中查找需要的特征 [peripheral discoverCharacteristics:nil forService:service]; } }
6. 根据特征的UUID从服务中所有特征中正确的特征;
/** 只要扫描到特征就会调用 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    // 拿到服务中所有的特诊
    NSArray *characteristics =  service.characteristics;
  
     遍历特征:
     if (正确的特征) {
         与外设交互代码;
      }
}
7. 利用特征与外设做数据交互;
8. 断开连接。
 
具体代码实现:
  1 /*存储所有的外设*/
  2 @property (nonatomic, strong) NSMutableArray *peripherals;
  3 /*中心服务管理者*/
  4 @property (nonatomic, strong) CBCentralManager *mgr;
  5 
  6 /*懒加载数组*/
  7 - (NSMutableArray *)peripherals{
  8     if (_peripherals == nil) {
  9         _peripherals = [NSMutableArray array];
 10     }
 11     return _peripherals;
 12 }
 13 
 14 - (void)viewDidLoad {
 15     [super viewDidLoad];
 16 
 17     //1创建中心管理者
 18     self.mgr = [[CBCentralManager alloc] init];
 19 
 20     //成为自己的代理
 21     mgr.delegate = self;
 22 
 23     //2利用中心设备扫描(数组指定的)外部设备
 24     /*  如果指定数组为空表示扫描所有的设备 */
 25     [mgr scanForPeripheralsWithServices:nil options:nil];
 26 }
 27 
 28 #pragma mark - CBCentralManagerDelegate代理方法
 29 /*发现外设的时候调用*/
 30 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
 31     
 32     // 判断如果数组中不包含当前扫描到得外部设置才保存
 33     if (![self.peripherals containsObject:peripheral]) {
 34 
 35         //让外设成为自己的代理
 36         peripheral.delegate = self;
 37 
 38         // 保存扫描到得外部设备
 39         [self.peripherals addObject:peripheral];
 40     }
 41 }
 42 
 43 /** 模拟连接所有的外设 */
 44 - (void)connect{
 45     //从保存的外设中取出所有的外设连接(也可以连接指定的外设)
 46     for (CBPeripheral *peripheral in self.peripherals) {
 47         /**  连接外设 */
 48         [self.mgr connectPeripheral:peripheral options:nil];
 49     }
 50 }
 51 
 52 /**
 53  *  连接外设成功调用
 54  */
 55 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
 56     // 扫描外设中得服务
 57     [peripheral discoverServices:nil];
 58 }
 60 
 61 /**
 62  *  连接外设失败调用
 63  */
 64 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
 65 }
 66 
 67 #pragma mark - CBPeripheralDelegate代理方法
 68 /**
 69  *  只要扫描到外设的服务就会调用
 70  *
 71  *  @param peripheral 服务所在的外设
 72  */
 73 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
 74     // 获取外设中所有扫描到得服务
 75     NSArray *services = peripheral.services;
 76     for (CBService *service in services) {
 77         // 拿到需要的服务
 78         if ([service.UUID.UUIDString isEqualToString:@"123456789abc"]) {
 79             // 从需要的服务中查找需要的特征
 80             // 从peripheral中得service中扫描特征
 81             [peripheral discoverCharacteristics:nil forService:service];
 82         }
 83     }
 84 }
 85 /**
 86  *  只要扫描到特征就会调用
 87  *
 88  *  @param peripheral 特征所属的外设
 89  *  @param service    特征所属的服务
 90  */
 91 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
 92 {
 93     // 拿到服务中所有的特诊
 94     NSArray *characteristics =  service.characteristics;
 95     // 遍历特征, 拿到需要的特征处理
 96     for (CBCharacteristic *characteristic in characteristics) {
 97         if ([characteristic.UUID.UUIDString isEqualToString:@"edg"]) {
 98             与外设的做数据交互的部分代码
 99         }
100     }
101 } 

 

posted @ 2016-03-21 14:23  zsper  阅读(543)  评论(0编辑  收藏  举报