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. ...
随机推荐
- oracle的存储过程语法(转)
1.ORA-00942: table or view does not exist 指的你要操作的表尚未存在,需要先create出来先. 2.ORA-00922: missing or invalid ...
- 【HDOJ】1728 逃离迷宫
题目大坑,注意行列顺序式反的,另外注意起点和终点相同. #include <iostream> #include <cstdio> #include <cstring&g ...
- bzoj3790
观察发现,这道题目其实就相当于一个最小区间覆盖问题这里的区间是指以每个点为中心的最长回文串很久没写manacher,有点感动不得不说manacher是一个非常好的算法 ..] of char; c,l ...
- (转载)Android开发者必知的开发资源
(转载)http://www.importnew.com/3988.html 随着Android平台市场份额的持续猛增 ,越来越多的开发者开始投入Android应用程序的开发大潮.如果您是一位2013 ...
- 数据结构(分块):[HZOI 2015]easy seq
[题目描述] 给定一个序列,下标从0开始,分别为a0,a1,a2...an−1,有m个询问,每次给出l和r,求满足ai=aj且l<=i<=j<=r时j−i的最大值 本题强制在线,l和 ...
- Linux自定义命令
linux自定义命令,就是给当前命令取个别名.比如:ls 列出当前的文件,rm + 文件名 就能删除该文件,如何自定义命令,可以使用alias比如:alias gobin='cd /opt/tomca ...
- [回顾]SVE回顾
SVE回顾 写完后的自评:书写太过凌乱,基本无法阅读. 前几日,SVE通过了TR5,虽说是一个小得不能再小的项目,即使到最后也存在一些未能解决的问题,但在用户的通融下还是在超期一段时间后写下了一个暂时 ...
- jQuery、实例大全
文章出处 http://www.cnblogs.com/suoning/p/5683047.html 一.简介 定义 jQuery创始人是美国John Resig,是优秀的Javascript框架: ...
- jQuery各种效果举例
jQuery 所有jQuery详细使用说明请见:http://www.php100.com/manual/jquery/ jQuery的作用是操作浏览器html,从而达到用户的可视化效果,按照功能可分 ...
- 去除 Visual Studio 中臃肿的 ipch 和 sdf 文件
使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...