CoreBluetooth
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的更多相关文章
- iOS蓝牙开发CoreBluetooth快速入门
在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...
- CoreBluetooth——IOS蓝牙4.0使用心得
原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...
- [iOS 基于CoreBluetooth的蓝牙4.0通讯]
一.首先大致介绍下蓝牙4.0的模式,中心和周边: 一般情况下,iPhone作为中心,接收来自周边传感器(比如手环等)采集的数据. 二.那整一个数据通讯的协议是怎样的呢? 为什么要一层层搞这么复杂呢?据 ...
- ios CoreBluetooth 警告 is being dealloc'ed while pending connection
ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...
- 蓝牙 CoreBluetooth
baseK(相关基础知识)蓝牙常见名称和缩写 BLE:(Bluetooth low energy)蓝牙4.0设备因为低耗电,也叫BLEperipheral,central:外设和中心设备,发起链接的是 ...
- CoreBluetooth - 中心模式
BLE中心模式流程-coding BLE中心模式流程 - 1.建立中心角色 - 2.扫描外设(Discover Peripheral) - 3.连接外设(Connect Peripheral) - 4 ...
- iOS CoreBluetooth 教程
去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...
- 蓝牙开发<coreBluetooth/CoreBluetooth.h>
/* 建立中心设备 扫描外设(Discover Peripheral) 连接外设(Connect Peripheral) 扫描外设中的服务和特征(Discover Services And Chara ...
- 蓝牙(CoreBluetooth)-外部设备(服务端)
蓝牙(CoreBluetooth)-外部设备(服务端) 主要内容 1. 创建外部管理器对象 2. 设置本地外设的服务和特征 3. 添加服务和特征到到你的设置的数据库中 4. 向外公布你的的服务 5. ...
随机推荐
- python装饰器之使用情景分析
http://blog.csdn.net/yueguanghaidao/article/details/10089181
- SVN 修改URL路径|SVN 项目路径修改
在svn的根目录下面右键 输入要修改的地址: 点击ok 搞定... ~~~
- 【HDOJ】2966 In case of failure
KD树,这东西其实在ML经常被使用,不过30s的时限还是第一次见. /* 2966 */ #include <iostream> #include <string> #incl ...
- 最棒的Visual Studio扩展
isual Studio是微软公司推出的开发环境,Visual Studio可以用来创建Windows平台下的Windows应用程序和网络应用程序,也可以用来创建网络服务.智能设备应用程序和Offic ...
- 【狼】openGL 光照的学习
小狼学习原创,欢迎批评指正 http://www.cnblogs.com/zhanlang96/p/3859439.html 先上代码 #include "stdafx.h" #i ...
- 模糊化GPU滤镜汇总
GPUImageTiltShiftFilter 这是一个模糊图片上下两层的滤镜效果,可以调节模糊边界,可以调节模糊程度 总共4个参数,具体如下 //模糊度的调节,0为最清晰,后面越来越模糊 ...
- cygwin with openssh
新建系统变量 CYGWIN=ntsec path添加 ;c:\cygwin\bin 之后参考http://blog.csdn.net/benkaoya/article/details/8884677 ...
- hdoj 3746 Cyclic Nacklace【KMP求在结尾加上多少个字符可以使字符串至少有两次循环】
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- thread.wait的一个好例子
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); HelloThread thread; thread.star ...
- oracle正则截取字符串的函数
现在有这么一个需求, 数据库中的一个手输的'籍贯'字段,要按一定的规范截取显示在报表上,比如,如果'籍贯'的内容是:'山东省潍坊市昌乐县', 那么报表里要显示为:'山东昌乐', 如果'籍贯'是山东省潍 ...