智能设备 和 app 通过 BLE通讯的两种模型

模型一:设备提供数据,app 展示数据; 比如小米手环

模型二:app提供数据,设备接收;

模型与corebluetooth的对应关系;

模型一:智能设备,对应 peripheral;app对应central

模型二:智能设备作为central ; app作为 peripheral

这里主要讨论模型一,这也是当前大多数手环设备和iOS 交互的方式;开发流程

1. 创建工程,导入CoreBluetooth.framework

2. 初始化 CBCentralManager 并准备扫描周围蓝牙设备

//初始化    
theManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; //初始化完成会调用如下代理,返回当前手机的蓝牙状态

//当前蓝牙主设备状态

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

if (central.state==CBCentralManagerStatePoweredOn) {

self.title = @"蓝牙已就绪";

self.connectBtn.enabled = YES;

}else

{

self.title = @"蓝牙未准备好";

[self.activeID stopAnimating];

switch (central.state) {

case CBCentralManagerStateUnknown:

NSLog(@">>>CBCentralManagerStateUnknown");

break;

case CBCentralManagerStateResetting:

NSLog(@">>>CBCentralManagerStateResetting");

break;

case CBCentralManagerStateUnsupported:

NSLog(@">>>CBCentralManagerStateUnsupported");

break;

case CBCentralManagerStateUnauthorized:

NSLog(@">>>CBCentralManagerStateUnauthorized");

break;

case CBCentralManagerStatePoweredOff:

NSLog(@">>>CBCentralManagerStatePoweredOff");

break;

default:

break;

}

}

}

3. 当蓝牙状态一切正常时,这时可以开始搜索周边设备了,比如手环

//开始连接action
- (IBAction)startConnectAction:(id)sender { if (theManager.state==CBCentralManagerStatePoweredOn) {
NSLog(@"主设备蓝牙状态正常,开始扫描外设...");
self.title = @"扫描小米手环...";
//nil表示扫描所有设备
[theManager scanForPeripheralsWithServices:nil options:nil];
[self.activeID startAnimating];
self.connectBtn.enabled = NO;
self.resultTextV.text = @""; } }

4. 在扫描到设备的代理里面,连接我们找到的智能设备

//扫描到设备会进入方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"扫描连接外设:%@ %@",peripheral.name,RSSI);
[central connectPeripheral:peripheral options:nil];
if ([peripheral.name hasSuffix:@"MI"]) {
thePerpher = peripheral;
[central stopScan];
[central connectPeripheral:peripheral options:nil];
self.title = @"发现小米手环,开始连接...";
self.resultTextV.text = [NSString stringWithFormat:@"发现手环:%@\n名称:%@\n",peripheral.identifier.UUIDString,peripheral.name];
}
}

5. 连接成功之后,去扫描设备的服务;扫描到服务之后,紧接着去扫描设备的特征;好比一个类提供的接口,只有知道这些,我们才能操作智能设备

//扫描到特征
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error)
{
NSLog(@"扫描外设的特征失败!%@->%@-> %@",peripheral.name,service.UUID, [error localizedDescription]);
self.title = @"find characteristics error.";
[self.activeID stopAnimating];
self.connectBtn.enabled = YES;
return;
} NSLog(@"扫描到外设服务特征有:%@->%@->%@",peripheral.name,service.UUID,service.characteristics);
//获取Characteristic的值
for (CBCharacteristic *characteristic in service.characteristics){
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic]; //步数
if ([characteristic.UUID.UUIDString isEqualToString:kMI_STEP])
{
[peripheral readValueForCharacteristic:characteristic];
} //电池电量
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_BUTERY])
{
[peripheral readValueForCharacteristic:characteristic];
} else if ([characteristic.UUID.UUIDString isEqualToString:kMI_SHAKE])
{
//震动
theSakeCC = characteristic;
} //设备信息
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_DEVICE])
{
[peripheral readValueForCharacteristic:characteristic];
} }
} } //扫描到服务
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error)
{
NSLog(@"扫描外设服务出错:%@-> %@", peripheral.name, [error localizedDescription]);
self.title = @"find services error.";
[self.activeID stopAnimating];
self.connectBtn.enabled = YES; return;
}
NSLog(@"扫描到外设服务:%@ -> %@",peripheral.name,peripheral.services);
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
NSLog(@"开始扫描外设服务的特征 %@...",peripheral.name); } #pragma mark 设备扫描与连接的代理
//连接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
self.title = @"连接成功,扫描信息...";
NSLog(@"连接外设成功!%@",peripheral.name);
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
NSLog(@"开始扫描外设服务 %@...",peripheral.name); }
//连接外设失败
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"连接到外设 失败!%@ %@",[peripheral name],[error localizedDescription]);
[self.activeID stopAnimating];
self.title = @"连接失败";
self.connectBtn.enabled = YES; } //与外设断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"与外设备断开连接 %@: %@", [peripheral name], [error localizedDescription]);
self.title = @"连接已断开";
self.connectBtn.enabled = YES;
}

6. 当我们对设备发送一些指令时,会在如下代理里面响应到设备返回的数据

#pragma mark 设备信息处理
//扫描到具体的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
if (error) {
NSLog(@"扫描外设的特征失败!%@-> %@",peripheral.name, [error localizedDescription]);
self.title = @"find value error.";
return;
}
NSLog(@"%@ %@",characteristic.UUID.UUIDString,characteristic.value);
if ([characteristic.UUID.UUIDString isEqualToString:kMI_STEP]) {
Byte *steBytes = (Byte *)characteristic.value.bytes;
int steps = TCcbytesValueToInt(steBytes);
NSLog(@"步数:%d",steps);
self.resultTextV.text = [NSString stringWithFormat:@"%@步数:%d\n",self.resultTextV.text,steps];
}
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_BUTERY])
{
Byte *bufferBytes = (Byte *)characteristic.value.bytes;
int buterys = TCcbytesValueToInt(bufferBytes)&0xff;
NSLog(@"电池:%d%%",buterys);
self.resultTextV.text = [NSString stringWithFormat:@"%@电量:%d%%\n",self.resultTextV.text,buterys]; }
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_DEVICE])
{
Byte *infoByts = (Byte *)characteristic.value.bytes;
//这里解析infoByts得到设备信息 } [self.activeID stopAnimating];
self.connectBtn.enabled = YES;
self.title = @"信息扫描完成"; }

7. 完整的代码工程;

https://github.com/cocoajin/BLE-miband

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

  1. iOS蓝牙4.0开发

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

  2. iOS 蓝牙4.0开发

    背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...

  3. iOS蓝牙4.0开发例子

    1建立中心角色 1 2 3 #import <CoreBluetooth/CoreBluetooth.h>  CBCentralManager *manager;  manager = [ ...

  4. 蓝牙4.0——Android BLE开发官方文档翻译

    ble4.0开发整理资料_百度文库 http://wenku.baidu.com/link?url=ZYix8_obOT37JUQyFv-t9Y0Sv7SPCIfmc5QwjW-aifxA8WJ4iW ...

  5. iOS蓝牙BLE4.0通信功能

    概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...

  6. 蓝牙4.0(BLE)开发

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/26740237 作者:小马 IOS学习也一段时间了,该上点干货了.前段时间研究了一下 ...

  7. Android低功耗蓝牙(蓝牙4.0)——BLE开发(上)

    段时间,公司项目用到了手机APP和蓝牙设备的通讯开发,这里也正好对低功耗蓝牙(蓝牙4.0及以后标准)的开发,做一个总结. 蓝牙技术联盟在2010年6月30号公布了蓝牙4.0标准,4.0标准在蓝牙3.0 ...

  8. iOS蓝牙4.0协议简单介绍

    iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...

  9. https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程

    The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...

随机推荐

  1. Java提高篇---Stack

    在Java中Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的.每一个栈都包含一个栈顶,每次出栈是将栈顶的数据取出,如下: Stack通过 ...

  2. 无边框窗体、用户控件、Timer控件

    一.无边框窗体1 最大化.最小化以及关闭按钮制作实际上就是更换点击前.指向时.点击时的图片 (1)将图片放在该文件夹的Debug中,获取图片的路径Application.StartupPath + & ...

  3. vim基本使用

    i 进入插入状态 esc 退出插入状态 x 删除一个字符 dd 删除一行,并拷贝 yy 拷贝 p 粘贴 u 撤销 ctrl+r 重做 :w 保存 :q 退出 :q! → 退出不保存

  4. ActiveMQ点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

  5. java中为什么byte的取值范围是-128到+127

    概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...

  6. SqlSever基础 dateadd day 增加五天

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  7. Linux的启动过程

    Linux的启动过程,也就是Linux的引导流程,这部分主要是理论知识. Linux的开机启动过程 1.1第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的 ...

  8. 我的android学习经历28

    一道题目关于Layout_weight: 当前屏幕的大小为430,有左右两个控件,未分配权重之前都是300,左控件的权重是3,右控件的权重是2,请计算左右两个控件的宽度大小是多少? 解: 当前屏幕剩余 ...

  9. winfrom增删改查

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. HDU 5030 Rabbit's String

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5030 题意:给出一个长度为n的串S,将S分成最多K个子串S1,S2,……Sk(k<=K).选出每 ...