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. ...
随机推荐
- 在Windows Server 2012 上安装Exchange 2013 服务器
前文:http://www.cnblogs.com/Liangw/archive/2011/09/19/2559944.html 安装准备: 1.加入一个存在的域(?如何建立一个域) 2.登录Wind ...
- POJ 1503 Integer Inquiry 简单大数相加
Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his explo ...
- HDOJ1002题A + B Problem II,2个大数相加
Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...
- Supervisord管理
原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...
- HashMap和Hashtable
HashTable 散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的 速 ...
- Android定义的路径全局变量
Android定义的路径全局变量 ifeq (,$(strip $(OUT_DIR))) OUT_DIR := $(TOPDIR)out endif DEBUG_OUT_DIR := $(OUT_DI ...
- hdu 4738 Caocao's Bridges (tarjan求桥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...
- 20个 Unix/Linux 命令技巧
让我们用这些Unix/Linux命令技巧开启新的一年,提高在终端下的生产力.我已经找了很久了,现在就与你们分享. 删除一个大文件 我在生产服务器上有一个很大的200GB的日志文件需要删除.我的rm和l ...
- God of War - HDU 2809(状态压缩+模拟)
题目大意:貌似是一个游戏,首先给出卢布的攻击,防御,还有血量,再给出每升一级增加的攻击防御还有血量,然后又N个敌人,杀死每个敌人都会得到一些经验,求杀死完所有敌人时剩余的最大血量. 分析:因为敌人比较 ...
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...