Core Bluetooth

Core Bluetooth的使用步骤

 Core Bluetooth的开发步骤

建立中心设备
扫描外设
连接外设
烧苗外设中的服务和特征
利用特征与外设做数据交互
断开链接


Core Bluetooth的开发步骤
建立中心设备
扫描外设
连接外设
烧苗外设中的服务和特征
利用特征与外设做数据交互
断开链接


Core Bluetooth的基本常识
每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的
一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征

特征是与外界交互的最小单位
比如说,一台蓝牙4.0设备,用特征A来描述自己的出厂信息,用特征B来收发数据
服务和特征都是用UUID来唯一标示的,通过UUID就能区别不同的服务和特征

设备里面各个服务和特征的功能,均由蓝牙设备硬件厂商提供,比如哪些是用来交互(读写),哪些可获取模块信息等 

 

示例代码

 //

//  ViewController.m
//  Core Bluetooth
//
//  Created by 奇集 on 15/6/25.
//  Copyright (c) 2015年 qiji. All rights reserved.
//

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
/**
 *  外设
 
*/
@property (nonatomic, strong) NSMutableArray *peripherals;
/**
 *  中心管理者
 
*/
@property (nonatomic, strong) CBCentralManager *mgr;

@end

@implementation ViewController
/**
 *  懒加载
 
*/

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


- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.创建中心设备
    CBCentralManager *mgr = [[CBCentralManager alloc] init];
    self.mgr = mgr;
    
    // 设置代理
    mgr.delegate = self;
    
    // 2.利用中心设备扫描指定的设备
    /*
     如果指定数组代表只扫描指定的设备,nil代表扫描所有设备
     
*/
    [mgr scanForPeripheralsWithServices:nil options:nil];
}

// 扫描设备代理方法
#pragma mark - CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    // 保存扫描到的外部设备
    
// 判断如果数组中不包含当前扫描到的外部设备才保存
    if (![self.peripherals containsObject:peripheral]) {
        peripheral.delegate = self;
        [self.peripherals addObject:peripheral];
    }
}

/**
 *  模拟点击,然后链接所有的外设
 
*/
- (void)start
{
    for (CBPeripheral *peripheral in self.peripherals) {
        // 链接外设
        [self.mgr connectPeripheral:peripheral options:nil];
    }
}

/**
 *  连接外设中的服务
 
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    // 扫描外设中的服务,nil代表扫描所有服务
    [peripheral discoverServices:nil];
}

/**
 *  连接外设失败时调用
 
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
    
}


#pragma mark - CBPeripheralDelegate
/**
 *  只要扫描到服务就会调用
 *  @param peripheral 服务所在的外设
 
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    // 获取外设中所有扫描到的服务
    NSArray *services = peripheral.services;
    for (CBService *service in services) {
        // 拿到需要的服务
        if ([service.UUID.UUIDString isEqualToString:@"123"]) {
            // 从需要的服务中查找需要的特征
            
// 从peripheral中得到service中扫描特征
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

/**
 *  只要扫描到特征就会调用
 *
 *  @param peripheral 特征所属的外设
 *  @param service    特征所属的服务
 
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    // 拿到服务中所有的特征
    NSArray *characteristics = service.characteristics;
    // 遍历特征,拿到需要的特征处理
    for (CBCharacteristic *characteristic in characteristics) {
        if ([characteristic.UUID.UUIDString isEqualToString:@"8888"]) {
            NSLog(@"设置闹钟");
        }
    }
}
@end

 

 

 

 

posted @ 2015-06-25 11:49  aprogrammer  阅读(169)  评论(0编辑  收藏  举报