iOS开发之蓝牙4.0开发使用
转载自https://www.jianshu.com/p/f0e4b0e98336
2015年的时候自己写过一个蓝牙4.0项目,当忘了写博客,最近看到这篇博客写的挺详细,所以转载一下
一: 介绍
蓝牙分为蓝牙2.0和蓝牙4.0。
蓝牙2.0为传统蓝牙,传统蓝牙也称为经典蓝牙。
蓝牙4.0因为低耗电,所以也叫做低功耗蓝(BLE),它将三种规格集一体,包括传统蓝牙技术、高速技术和低耗能技术。
这篇文章用来介绍BLE 4.0的使用以及相关问题的解决。
二:BLE的两种模式
BLE的两种模式分为CBCentralMannager 中心模式 和CBPeripheralManager 外设模式,在这里主要和大家分享CBCentralMannager 中心模式的开发和使用。
CBCentralMannager 中心模式
以手机(app)作为中心,连接其他外设的场景。详细流程如下:
- 建立中心角色
- 扫描外设
- 发现外设
- 连接外设
4.1 连接失败
4.2 连接断开
4.3 连接成功 - 扫描外设中的服务
5.1 发现并获取外设中的服务 - 扫描外设对应服务的特征
6.1 发现并获取外设对应服务的特征
6.2 给对应特征写数据 - 订阅特征的通知
7.1 根据特征读取数据
CBPeripheralManager 外设模式
使用手机作为外设连接其他中心设备操作的场景。
PS:因为苹果设备的安全性和封闭性,苹果设备不能通过与其他设备蓝牙链接进行文件传输等功能,所以在iOS与蓝牙开发的编程中是CBCentralMannager 中心模式编程居多.
- 建立外设角色
- 设置本地外设的服务和特征
- 发布外设和特征
- 广播服务
- 响应中心的读写请求
- 发送更新的特征值,订阅中心
三:BLE开发步骤
在介绍CBCentralMannager 中心模式开发步骤之前,首先需要对项目进行如下配置:
#import "ESPFBYBLEHelper.h" #import <CoreBluetooth/CoreBluetooth.h>@interface ESPFBYBLEHelper ()<CBCentralManagerDelegate,CBPeripheralDelegate> // 中心管理者(管理设备的扫描和连接) @property (nonatomic, strong) CBCentralManager *centralManager; // 存储的设备 @property (nonatomic, strong) NSMutableArray *peripherals; // 扫描到的设备 @property (nonatomic, strong) CBPeripheral *cbPeripheral; // 外设状态 @property (nonatomic, assign) CBManagerState peripheralState; @end// 蓝牙4.0设备名 static NSString * const kBlePeripheralName = @"lighte290"; // 通知服务 static NSString * const kNotifyServerUUID = @"FF03"; // 写服务 static NSString * const kWriteServerUUID = @"FFFF"; // 通知特征值 static NSString * const kNotifyCharacteristicUUID = @"FF05"; // 写特征值 static NSString * const kWriteCharacteristicUUID = @"FF08"; @implementation ESPFBYBLEHelper
这其中需要导入CoreBluetooth框架
#import <CoreBluetooth/CoreBluetooth.h>
遵守CBCentralManagerDelegate,CBPeripheralDelegate协议
@interface ESPFBYBLEHelper ()<CBCentralManagerDelegate,CBPeripheralDelegate>
然后需要检测蓝牙状态,代码如下:
// 状态更新时调用 - (void)centralManagerDidUpdateState:(CBCentralManager *)central {switch (central.state) {case CBManagerStateUnknown:{NSLog(@"为知状态");self.peripheralState = central.state;}break;case CBManagerStateResetting:{NSLog(@"重置状态");self.peripheralState = central.state;}break;case CBManagerStateUnsupported:{NSLog(@"不支持的状态");self.peripheralState = central.state;}break;case CBManagerStateUnauthorized:{NSLog(@"未授权的状态");self.peripheralState = central.state;}break;case CBManagerStatePoweredOff:{NSLog(@"关闭状态");self.peripheralState = central.state;}break;case CBManagerStatePoweredOn:{NSLog(@"开启状态-可用状态");self.peripheralState = central.state;NSLog(@"%ld",(long)self.peripheralState);}break;default:break;} }
添加属性和常量,常量需要根据自己的项目来进行配置。
下面只需要根据实现流程一步步实现即可,核心代码如下:
1. 建立中心角色
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
2. 扫描外设
if (self.peripheralState == CBManagerStatePoweredOn){[self.centralManager scanForPeripheralsWithServices:nil options:nil]; }
3. 发现外设
/**扫描到设备@param central 中心管理者@param peripheral 扫描到的设备@param advertisementData 广告信息@param RSSI 信号强度*/ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {NSLog(@"%@",[NSString stringWithFormat:@"发现设备,设备名:%@",peripheral.name]); }
4. 连接外设
[self.centralManager connectPeripheral:peripheral options:nil];
- 4.1 连接失败 didFailToConnectPeripheral
/**连接失败@param central 中心管理者@param peripheral 连接失败的设备@param error 错误信息*/ - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {NSLog(@"%@",@"连接失败"); }
- 4.2 连接断开
/**连接断开@param central 中心管理者@param peripheral 连接断开的设备@param error 错误信息*/ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {NSLog(@"%@",@"断开连接"); }
- 4.3 连接成功
/**连接成功@param central 中心管理者@param peripheral 连接成功的设备*/ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {NSLog(@"连接设备:%@成功",peripheral.name);[self.centralManager stopScan]; }
5. 扫描外设中的服务
// 设置设备的代理 peripheral.delegate = self; // services:传入nil 代表扫描所有服务 [peripheral discoverServices:nil];
5.1 发现并获取外设中的服务
/**扫描到服务@param peripheral 服务对应的设备@param error 扫描错误信息*/ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {// 遍历所有的服务for (CBService *service in peripheral.services){NSLog(@"服务:%@",service.UUID.UUIDString);} }
6. 扫描外设对应服务的特征
// 获取对应的服务if (![service.UUID.UUIDString isEqualToString:kWriteServerUUID]){return;}// 根据服务去扫描特征[peripheral discoverCharacteristics:nil forService:service];
6.1 发现并获取外设对应服务的特征
/**扫描到对应的特征@param peripheral 设备@param service 特征对应的服务@param error 错误信息*/ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {NSLog(@"%@",peripheral); }
6.2 给对应特征写数据
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
7. 订阅特征的通知
if ([characteristic.UUID.UUIDString isEqualToString:kNotifyCharacteristicUUID]){[peripheral setNotifyValue:YES forCharacteristic:characteristic]; }
7.1 根据特征读取数据 didUpdateValueForCharacteristic
/**根据特征读到数据@param peripheral 读取到数据对应的设备@param characteristic 特征@param error 错误信息*/ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error {if ([characteristic.UUID.UUIDString isEqualToString:kNotifyCharacteristicUUID]){NSData *data = characteristic.value;NSLog(@"%@",data);} }
转载于:https://www.cnblogs.com/hecanlin/p/11578097.html
iOS开发之蓝牙4.0开发使用相关推荐
- 蓝牙4.0android开发,Android蓝牙4.0开发
添加蓝牙权限 特性请求 值为true则指定设备必须带BLE才能安装该应用程序 值为false则不论设备是否带BLE都可以安装应用程序(但运行时必须判断设备是否支持该特性 判断设备是否支持BLE(如果特 ...
- iOS开发之蓝牙4.0技术完美实现
CSDN博客 BaiHuaXiu123 博客专家 iOS开发之蓝牙4.0技术完美实现 发表于2016/5/1 21:13:06 8034人阅读 前言 前端时间,同学在做项目过程中遇到 ...
- 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受
谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受 谈谈几个月以来开发android蓝牙4.0 BLE低功耗应用的感受,注明下时间:2012-10-17写的博客,后期更新的也注明了时间 ...
- 开发android蓝牙4.0 BLE低功耗应用的感受
文章转自: http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html 谈谈几个月以来开发android蓝 ...
- iOS 蓝牙4.0开发使用(内附Demo)
一: 介绍 近几年,智能设备越来越火,这些智能设备中,有很大一部分是通过手机来控制硬件设备,来达到预期的效果,这中间少不了要使用到蓝牙功能,通过蓝牙来通信来控制设备. 蓝牙分为蓝牙2.0和蓝牙4.0. ...
- iOS 蓝牙 4.0 开发
随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的发展史也不难看出 ...
- iOS 蓝牙4.0开发
1. 什么是蓝牙 4.0 ? 全新的蓝牙4.0技术并不是一种技术,而是由传统蓝牙,高速蓝牙和低功耗蓝牙合而为一.并且这三种蓝牙可以组合使用,也可以单独使用.其中,低功耗蓝牙即BLE是蓝牙4.0的核 ...
- Android BLE蓝牙4.0开发 实现扫描、连接、通讯、获取通知、特性等 (一、打开蓝牙 进行扫描)
目录 首先说下蓝牙4.0 目前android蓝牙有2种: ble蓝牙开发流程: 1.首先当然是进行权限申请啦: 2.获取蓝牙适配器 BluetoothAdapter 3.打开蓝牙 4.关闭蓝牙 5.在 ...
- Android BLE蓝牙4.0开发详解
这篇博客主要讲解 蓝牙 BLE 的用法.在讲解之前先讲一些概念性的东西,对于之前没接触过蓝牙开发,现在手上又有个蓝牙BLE项目需要做的人,先看下这些概念还是很重要的.因为我之前就是这样,之前没有接触过 ...
最新文章
- Access restriction: The type BASE64Encoder is not accessible due to restrict
- vue项目中使用swiper
- SDL_main导致main找不到入口
- 世界首个机器人观音在岛国问世,请问AI开光还会远吗?
- ArcGIS API for JavaScript 加载水经注离线地图
- c语言倒序输出单词_英语句子按单词倒序输出C语言版
- idea工作空间多开项目
- netty 高匿ip检测_高匿代理ip不用担心被网站检测
- 疫情下绩效管理和奖金激励怎么做
- 适用于 Windows 的 iTunes 10.12.9.3 64位 最新版 下载,支持 iOS 14
- intellij背景护眼色调节
- SwiftUI 界面大全之IOT物联网复杂管理界面(教程含源码)
- 长微博工具开发者称遭新浪无故封号
- Altium Designer 2020 如何修改现有原理图图纸尺寸
- android阅读软件怎么开发,Android平台手机文本阅读软件的开发与设计.doc
- Java 基础语法 + 常见缩写单词全称(含中文翻译)
- 独孤思维:小白搬运项目,房产公众号月入10w+
- 【LDAP】在Centos7环境搭建LDAP服务端
- 【JavaSE】之网络编程
- 在html中使用ajax