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. ...
随机推荐
- 【HDOJ】3183 A Magic Lamp
RMQ. /* 3183 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...
- bzoj2734
非常巧妙地题目对于一个数x列出这样的矩阵x 2x 4x 8x ……3x 6x 12x 24x ………………………………不难方案数就是求取数不相邻的方案数考虑矩阵宽不超过logn,所以可以用状压dp解决 ...
- 【异步编程】when.js
异步编程:When.js快速上手 var api = 'http://qgy18.imququ.com/file/when/d.php?cb=?'; var getData = function() ...
- 【转】XCode快捷键
原文网址:http://www.cnblogs.com/yjmyzz/archive/2011/01/25/1944325.html 1. 文件 CMD + N: 新文件CMD + SHIFT + N ...
- IronPython fail to add reference to WebDriver.dll
在使用Ironpython引用WebDriver程序集做web自动化时碰到这个问题,出问题的代码很简单,如下: import sys import clr clr.AddReferenceToFile ...
- JVM中锁优化简介
本文将简单介绍HotSpot虚拟机中用到的锁优化技术. 自旋锁 互斥同步对性能最大的影响是阻塞的实现,挂起线程和恢复线程的操作都需要转入内核态中完成,这些操作给系统的并发性能带来了很大的压力.而在很多 ...
- TOYS - POJ 2318(计算几何,叉积判断)
题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方 ...
- hdoj 3342 Legal or Not【拓扑排序】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- windows系统下搭建linux
1.先装虚拟机VMware Workstation(步骤参照度娘) 2.在虚拟机上装CentOS6.5Linux系统(步骤参照度娘) 3.安装SecureCRT终端仿真程序,用来登录Linux服务 ...
- disconf实践(二)
因为有些系统的配置文件会随着业务更改,如某些控制开关,当大批量集群时,按照上一篇文章的配置就不够啦,需要做到热加载. 研究了一下,还好,比较简单,只要替换上一篇文章第4步的配置文件(spring-di ...