iOS 蓝牙开发之(CoreBlueTooth)
CoreBlueTooth
简介:
可用于第三方的蓝牙交互设备 设备必须支持蓝牙4.0
iPhone的设备必须是4S或者更新
iPad设备必须是iPad mini或者更新
iOS的系统必须是iOS 6或者更新
蓝牙4.0以低功耗著称,所以一般被称为BLE(bluetooth low energy)
核心概念
CBCenterManager:中心设备(用来连接到外部设备的管家)
CBPeripheralManager:外部设备(第三方的蓝牙4.0设备 用来扫描服务和服务特征)

开发步骤
1.建立中心管家
#import <CoreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
//中心管家
@property (nonatomic,strong) CBCentralManager *manager;
//存储扫描到的外部设备
@property (nonatomic,strong) CBPeripheral *peripheral; @end
/*
1.建立中心管家
2.扫描外部设备
3.连接外部设备
4.扫描服务和特征
5.数据交互
6.断开连接
*/
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//建立中心管家 queue 填空 默认为主线程
CBCentralManager *manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.manager = manager;
}
2.扫描外设
//在代理方法中扫描外部设备
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
//在开机状态下才能扫描外部设备
if (central.state == CBManagerStatePoweredOn) {
//扫描外部设备的哪些服务
//scanForPeripheralsWithServices 传空的话 可以扫描外部所有可以发现的设备 否则只扫描传入的相应ID的设备
[self.manager scanForPeripheralsWithServices:nil options:nil];
}
}
3.连接外设
//已经找到了外部设备 连接外部设备 我们也可以保存这些外部设备到一个数组 这里不做操作 也可以在这里对外部设备进行筛选
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"peripheral = %@",peripheral);
//保存外部设备
self.peripheral = peripheral;
self.peripheral.delegate = self;
//连接外部设备
[self.manager connectPeripheral:peripheral options:nil];
}
外设连接成功
//已经连接到了外部设备 开始扫描该设备服务
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { //扫描设备的服务 调用CBPeripheralDelegate
[self.peripheral discoverServices:nil];
}
4.扫描外设服务和特征
每个蓝牙4.0的设备都是通过服务和特征来展示自己的,一个设备必然包含一个或多个服务 一个服务包含一个或多个特征。
#pragma mark - CBPeripheralDelegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
//扫描到的服务
NSLog(@"扫描到的服务:%@",peripheral.services); for (CBService *service in peripheral.services) {
//由于有很多服务 我们可以去特定的服务 做特定的功能
// if ([service.UUID.UUIDString isEqualToString:@"Battery"]) {
// [self.peripheral discoverCharacteristics:nil forService:service];
// }
//扫描服务特征
[self.peripheral discoverCharacteristics:nil forService:service];
}
}
发现服务对应的特征
/**
* 发现服务对应的特征
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// 服务对应的特征
NSArray *ctcs = service.characteristics;
// 遍历所有的特征
for (CBCharacteristic *character in ctcs) {
// 根据特征的唯一标示过滤
if ([character.UUID.UUIDString isEqualToString:@"XMG"]) {
NSLog(@"可以吃饭了");
}
}
}
特征描述
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"descriptor = %@",characteristic.descriptors);
for (CBDescriptor *descriptor in characteristic.descriptors) {
//读取特征描述
[self.peripheral readValueForDescriptor:descriptor];
}
}
5.与外设做数据的交互 读或写
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"更新特征值%@时发生错误:%@", characteristic.UUID, [error localizedDescription]);
return;
}
// 收到数据
[delegate didGetDataForString:[self hexadecimalString:characteristic.value]];
// NSLog(@"%@",[self hexadecimalString:characteristic.value]);
}
数据的转换
我们接收到的数据,正是characteristic.value,这是一个NSData类数据,我们可以通过UTF8StringEncoding来转化为NSString,为了代码结构清晰,我专门把NSData和NSString互转写成了两个方法:
//将传入的NSData类型转换成NSString并返回
- (NSString*)hexadecimalString:(NSData *)data{
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return result;
}
//将传入的NSString类型转换成NSData并返回
- (NSData*)dataWithHexstring:(NSString *)hexstring{
NSData *aData;
return aData = [hexstring dataUsingEncoding: NSASCIIStringEncoding];
}
写的比较基础,另附大神写的比较全面的蓝牙博客地址
http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-0.html
iOS 蓝牙开发之(CoreBlueTooth)的更多相关文章
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- iOS 蓝牙开发资料记录
一.蓝牙基础认识: 1.iOS蓝牙开发: iOS蓝牙开发:蓝牙连接和数据读写 iOS蓝牙后台运行 iOS关于app连接已配对设备的问题(ancs协议的锅) iOS蓝牙空中 ...
- iOS蓝牙开发(一)蓝牙相关基础知识(转)
转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...
- iOS蓝牙开发
蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...
- iOS蓝牙开发(4.0)详解
最近由于项目需要, 一直在研究蓝牙4.0,在这儿分享给大家, 望共同进步. 一.关于蓝牙开发的一些重要的理论概念: 1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/Core ...
- iOS 蓝牙开发详解
目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另 ...
- ios蓝牙开发(一)蓝牙相关基础知识
蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...
- iOS蓝牙开发总结-4
蓝牙开发总结 只要熟悉蓝牙的流程,和蓝牙中每一个角色的作用,其实蓝牙通讯并没有想象中的难 1.蓝牙中心CBCentralManager:一般指得是iPhone手机 2.设备(外设)CBPeripher ...
- iOS蓝牙开发CoreBluetooth快速入门
在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...
- ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。
手机app去读写蓝牙设备....... 代码下载: 原文博客主提供Github代码连接,地址是:https://github.com/coolnameismy/demo ios连接外设的代码实现流程: ...
随机推荐
- 关于Docker&kubernetes的一些问题
本文是我自己在学习docker以及kubernetes的过程中遇到的一些问题,以及同事在听过培训之后一些问题,事后我自己去网上找些资料以及问一些资深大牛,我在此做一个归纳总结,将这些问题的解答做一个分 ...
- 【Javascript 基础】对象
1 创建对象 Javascript 支持对象的概率.有多种方法可以用来创建对象. <!DOCTYPE html> <html lang="en"> < ...
- Angular 学习笔记——模块之间的通讯
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- AFN检測网络情况
问: I'm a bit lost on AFNetorking's Reachability and haven't found a lot of good information out ther ...
- Eclipse对于多个Java项目的支持并不友好!
本文吐槽! 如果我们创建两个Java项目.一个叫StatsReader.把数据从网上下载到本地数据库里.一个叫StatsViewer.把数据从数据库里拿出来呈现给用户.这两个项目都要用同一个外部类库m ...
- MySQL性能优化的最佳20+条经验(转)
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序 员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...
- Struts2学习小结
1 基础 使用:导入 jar 包,配置 web.xml,并引入 struts.xml 文件 DMI:动态方法调用,调用时使用!分隔 action 名与方法名,如 index ! add.action, ...
- Burp Suite Intruder的4种攻击类型
位置:Intruder>1(通常为数字)>Positions,Attack Type下拉有四种,分别为 一.Sniper(狙击手模式) 狙击手模式使用一组payload集合,它一次只使用一 ...
- python常见面试题(二)
1. 到底什么是Python?你可以在回答中与其他技术进行对比(也鼓励这样做). 下面是一些关键点: Python是一种解释型语言.这就是说,与C语言和C的衍生语言不同,Python代码在运行之前不需 ...
- 【NOI2015】【寿司晚宴】【状压DP】
Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴. 小 G 和小 W 作为參加 NOI 的选手,也被邀请參加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不 ...