Core Bluetooth的基本常识

每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的

一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征

特征是与外界交互的最小单位

比如说,一台蓝牙4.0设备,用特征A来描述自己的出厂信息,用特征B来收发数据

服务和特征都是用UUID来唯一标识的,通过UUID就能区别不同的服务和特征

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

******************************************************************************************

Core Bluetooth的开发步骤

建立中心设备

扫描外设(Discover Peripheral)

连接外设(Connect Peripheral)

扫描外设中的服务和特征(Discover Services And Characteristics)

利用特征与外设做数据交互(Explore And Interact)

断开连接(Disconnect)

 #import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h> @interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate>
/**
* 外设
*/
@property (nonatomic, strong) NSMutableArray *peripherals;
/**
* 中心管理者
*/
@property (nonatomic, strong) CBCentralManager *mgr;
@end @implementation ViewController #pragma mark - 懒加载
// 1.创建中心设备管理对象
- (CBCentralManager *)mgr
{
if (_mgr == nil) {
_mgr = [[CBCentralManager alloc] init];
_mgr.delegate = self;
}
return _mgr;
} - (NSMutableArray *)peripherals
{
if (_peripherals == nil) {
_peripherals = [NSMutableArray array];
}
return _peripherals;
} #pragma mark - 系统方法
- (void)viewDidLoad {
[super viewDidLoad]; // 2.扫描外设
[self.mgr scanForPeripheralsWithServices:nil options:nil];
} /**
* 模拟点击, 连接外设
*/
- (void)start
{
for (CBPeripheral *peripheral in self.peripherals) {
// 4.连接设备
[self.mgr connectPeripheral:peripheral options:nil];
}
} #pragma mark - CBCentralManagerDelegate
/**
* 扫描到外设的时候调用
*
* @param central 中心设备管理对象
* @param peripheral 外设
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// 3.保存外设
if (![self.peripherals containsObject:peripheral]) {
peripheral.delegate = self;
[self.peripherals addObject:peripheral];
} } /**
* 连接到外设的时候调用
*
* @param central 中心设备管理对象
* @param peripheral 外设
*/
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
// 5.扫描外设中的服务
[peripheral discoverServices:nil];
} #pragma mark - CBPeripheralDelegate
/**
* 只要扫描到服务就会调用
*
* @param peripheral 服务所在的外设
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{ for (CBService *service in peripheral.services) {
if ([service.UUID.UUIDString isEqualToString:@""]) {
// 6.扫描指定服务的特征
[peripheral discoverCharacteristics:nil forService:service];
}
}
} /**
* 发现特征时调用
*
* @param peripheral 外设
* @param service 服务
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
for (CBCharacteristic *characteristic in service.characteristics) {
// 获取指定特征后进行数据交互
if ([characteristic.UUID.UUIDString isEqualToString:@""]) {
NSLog(@"设置闹钟");
// 7.停止扫描
[self.mgr stopScan];
}
}
}

CoreBluetooth的更多相关文章

  1. iOS蓝牙开发CoreBluetooth快速入门

    在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...

  2. CoreBluetooth——IOS蓝牙4.0使用心得

    原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...

  3. [iOS 基于CoreBluetooth的蓝牙4.0通讯]

    一.首先大致介绍下蓝牙4.0的模式,中心和周边: 一般情况下,iPhone作为中心,接收来自周边传感器(比如手环等)采集的数据. 二.那整一个数据通讯的协议是怎样的呢? 为什么要一层层搞这么复杂呢?据 ...

  4. ios CoreBluetooth 警告 is being dealloc'ed while pending connection

    ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...

  5. 蓝牙 CoreBluetooth

    baseK(相关基础知识)蓝牙常见名称和缩写 BLE:(Bluetooth low energy)蓝牙4.0设备因为低耗电,也叫BLEperipheral,central:外设和中心设备,发起链接的是 ...

  6. CoreBluetooth - 中心模式

    BLE中心模式流程-coding BLE中心模式流程 - 1.建立中心角色 - 2.扫描外设(Discover Peripheral) - 3.连接外设(Connect Peripheral) - 4 ...

  7. iOS CoreBluetooth 教程

    去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...

  8. 蓝牙开发<coreBluetooth/CoreBluetooth.h>

    /* 建立中心设备 扫描外设(Discover Peripheral) 连接外设(Connect Peripheral) 扫描外设中的服务和特征(Discover Services And Chara ...

  9. 蓝牙(CoreBluetooth)-外部设备(服务端)

    蓝牙(CoreBluetooth)-外部设备(服务端) 主要内容 1. 创建外部管理器对象 2. 设置本地外设的服务和特征 3. 添加服务和特征到到你的设置的数据库中 4. 向外公布你的的服务 5. ...

随机推荐

  1. Bluetooth LE(低功耗蓝牙) - 第二部分

    回顾 在前面的文章中我们介绍了Bluetooth LE的背景也说明了我们在本系列文章中将要开发什么,但是还没有实际的代码.我们将在这篇文章中纠正这一点,我们将通过定义 Service/Activity ...

  2. Android Studio启动时Fetching android sdk component information超时的解决方案

    1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开. 2)在idea.properties文件末尾添加一行: disable.an ...

  3. Microsoft Windows 远程权限提升漏洞(CVE-2013-3175)(MS13-062)

    漏洞版本: Microsoft Windows XP Microsoft Windows Vista Microsoft Windows Server 2008 Microsoft Windows R ...

  4. WordPress Download Monitor插件跨站脚本漏洞

    漏洞名称: WordPress Download Monitor插件跨站脚本漏洞 CNNVD编号: CNNVD-201308-139 发布时间: 2013-08-14 更新时间: 2013-08-14 ...

  5. 把这两天遇到的码(e)农(xin)题记下来

    1019: [SHOI2008]汉诺塔 1858: [Scoi2010]序列操作 1058: [ZJOI2007]报表统计

  6. javascipt取整数四舍五入

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...

  7. Performance testing of web application

    Testing the performance of web application is easy . It's easy to design unrealistic scenario . Easy ...

  8. 《A First Course in Probability》-chape1-组合分析-二项式定理

    二项式系数的概念给人最直观的概念就是,这里有n个物品,分成两组,其中一组的数量是i的所有组合情况. 它的证明过程既可以从组合分析的角度,也可以从数学归纳的角度,由于数学归纳涉及到计算比较困难,我们这里 ...

  9. lightoj 1021 - Painful Bases 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1021 #include<cstring> #include<cstd ...

  10. Hadoop Hive概念学习系列之什么是Hive?(一)

    参考  <Hadoop大数据分析与挖掘实战>的在线电子书阅读                   http://yuedu.baidu.com/ebook/d128cf8e33687e21 ...