蓝牙是一个标准的无线通讯协议,具有设备成本低、传输距离近和功耗低等特点,被广泛的应用在多种场合。蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近、功耗相对大;而BLE则用来传输节点数据,传输数据量十分小,多数情况处于休眠状态,因而功耗十分低,被广泛的应用于智能穿戴设备。

蓝牙BLE简介

本文主要介绍iOS的蓝牙BLE开发流程,在介绍具体开发流程之前,有必要了解一下蓝牙BLE的特点。BLE通过属性(attribute)在clientserver之间进行数据交互,GATT定义了属性协议(Profile)来进行发现设备、读写数据和获取状态等功能。其中,在iOS蓝牙BLE开发过程中,App应用属于Central设备,BLE产品属于外设PeripheralProfile的结构图如下:

其中,ServiceCharacteristic 都有一个UUID来相互区分,类似心跳、血糖等的ServiceUUID由蓝牙SIG统一设定,同时也允许自定义服务,但仍需要用不同的UUID来标识。

针对客户端蓝牙BLE开发,一般不需要深入了解蓝牙协议栈,如果有兴趣,可以参考如下资料(本资料来自TI):

TI_BLE_Description

BLE开发流程

1. 创建CBCentralManager

创建一个队列,然后在这个队列里面进行BLE的各种操作

	//创建CBCentralManager对象
dispatch_queue_t queue = dispatch_queue_create("bluetooth", DISPATCH_QUEUE_SERIAL);
CBCentralManager *mgr = [[CBCentralManager alloc] initWithDelegate:self queue:queue];

2. 扫描外设

参数介绍:

  • serviceUUIDs: 指定扫描包含特点服务的外设,传nil表明是所有服务
  • options: 扫描时的设置,是一个字典
	//CBCentralManagerScanOptionAllowDuplicatesKey值为 No,表示不重复扫描已发现的设备
NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[_mgr scanForPeripheralsWithServices:nil options:optionDic];

3. 停止扫描

	[_mgr stopScan];

4. 连接外设

遍历扫描到的外设,然后连接外设

	for (CBPeripheral *peripheral in self.peripherals) {
[_mgr connectPeripheral:peripheral options:nil];
}

5. 扫描外设中的服务和特征

获取服务

	[peripheral discoverServices:nil];

获取特征

	[peripheral discoverCharacteristics:nil forService:service];

获取描述

	[peripheral discoverDescriptorsForCharacteristic:characteristic]

改写特征数据

	[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

6. 部分CBCentralManagerDelegate方法简介

代理方法:centralManagerDidUpdateState

Central已经更新状态,要在CBManagerStatePoweredOn里扫描外设,因为这是蓝牙初始化工作已完成

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state) {
case CBManagerStatePoweredOn:
{
NSLog(@"开启蓝牙, 开始扫描"); [_mgr scanForPeripheralsWithServices:nil options:nil];
}
break;
case CBManagerStateUnsupported:
NSLog(@"不支持蓝牙");
break;
case CBManagerStatePoweredOff:
NSLog(@"蓝牙未打开");
break; default:
NSLog(@"蓝牙打开失败");
break;
}
}

代理方法: centralManager:didDiscoverPeripheral:advertisementData:RSSI:

扫描到外设

	- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI

代理方法: centralManager:didConnectPeripheral:

连接到外设

	- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

7. 部分CBPeripheralDelegate方法简介

代理方法: peripheral:didDiscoverServices:

发现服务

	- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

代理方法: peripheral:didDiscoverCharacteristicsForService:error:

发现服务的特征

	- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

代理方法: peripheral:didUpdateValueForCharacteristic:error:

已经更新特征的值

	- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

代理方法: peripheral:didWriteValueForCharacteristic:error:

已经写入特征的值

	-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

8. 实例代码

下面的视图是基于小米手环2的测试数据,由于不是小米手环的开发者,没办法读取详细的数据,只把硬件、软件的版本信息等数据读出,以供需要开发蓝牙BLE之参考。




参考源码

https://github.com/BirdandLion/iOS-BLE.git

参考文档

http://www.jianshu.com/p/0a6c49922aad

http://www.jianshu.com/p/4df85eba6dab

http://www.jianshu.com/p/7ba443878e7d

iOS蓝牙BLE开发的更多相关文章

  1. Android蓝牙BLE开发,扫描、连接、发送和读取信息;

    1.BLE开发权限 Android蓝牙BLE开发须打开蓝牙权限和6.0位置权限: <uses-permission android:name="android.permission.B ...

  2. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...

  3. Android 蓝牙 BLE 开发笔记

    最近公司头戴换了一块蓝牙4.0 BLE模块,所以我们Android组要适配 BLE.Android BLE 需要 4.3 以上系统,api 还是非常简单的, 第一步就是扫描, 扫描到设备后就可以连接了 ...

  4. Android 低功耗蓝牙BLE 开发注意事项

    基本概念和问题 1.蓝牙设计范式? 当手机通过扫描低功耗蓝牙设备并连接上后,手机与蓝牙设备构成了客户端-服务端架构.手机通过连接蓝牙设备,可以读取蓝牙设备上的信息.手机就是客户端,蓝牙设备是服务端. ...

  5. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  6. iOS蓝牙开发(一)蓝牙相关基础知识(转)

    转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...

  7. iOS蓝牙开发

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

  8. iOS蓝牙4.0开发

    文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...

  9. iOS 蓝牙开发详解

    目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另 ...

随机推荐

  1. 使用jQuery操作 DOM

    DOM操作分为三类: 1.DOM Core:任何一种支持DOM的编程语言都可以使用它,如getElementById() 2.HTML-DOM:用于处理HTML文档,如document.forms 3 ...

  2. NewsDao

    package com.pb.news.dao; import java.util.Date;import java.util.List; import com.pb.news.entity.News ...

  3. IT小天博客APP版本

    今天弄了一个博客的APP版本,感觉还不错,欢迎下载体验. 共 1 张图片 APP名称:[IT小天博客APP] APP版本:1.3 APP上线时间:2017-06-29 下载地址:点击下载

  4. set-集合功能介绍

    叨逼叨:#集合 不可重复的列表 可变类型#1.添加 无则添加有则不操作 不可重复 # se = {'alex','eric','seven'} # se.add('qiqi') # se.add('b ...

  5. 前端模块化工具--webpack使用感受

    话说前头 webpack前段时间有听说一下,现在已经到了3.x的版本,自己没去接触.因为之前使用gulp来作为自己的项目构建工具.现在感觉gulp使用的趋势在减少.现在这段时间去接触了webpack, ...

  6. poj_3461: Oulipo

    题目链接 基础KMP题,本文提供一段能AC并且便于调试以及查看next数组的代码. 参考博客 http://blog.csdn.net/v_july_v/article/details/7041827 ...

  7. 【转载】java InputStream读取数据问题

    原文链接:http://www.cnblogs.com/MyFavorite/archive/2010/10/19/1855758.html 1. 关于InputStream.read()     在 ...

  8. 在 Linux 命令行脚本中执行 sudo 时自动输入密码

    使用 expect 实现自动登录的脚本的原理. 脚本代码如下: ############################################## #!/usr/bin/expect set ...

  9. 初次配置git与github出现push不了的问题

    ssh: connect to host gmail.com port 22: No route to host fatal: Could not read from remote repositor ...

  10. (转)Spring注解完成Bean的定义

    使用Spring注解完成Bean的定义 2010-04-21 16:48:54|  分类: spring|举报|字号 订阅     下载LOFTER客户端     通过@Autowired或@Reso ...