iOS蓝牙交互(4.0)
- 前期准备工作:查看coreBlueTooth框架,查看一些蓝牙名词:Central(中心设备)、Peripheral(外围设备)、advertisementData(广播数据)、-RSSI: 信号强弱值、Services(服务)、Characteristic(特征);
- 实例化对象以及遵守代理
.1设置对象
@property (strong, nonatomic) CBCentralManager *CBManager;
@property (strong, nonatomic) CBPeripheral *peripheral;
遵守对应的代理:CBCentralManagerDelegate,CBPeripheralDelegate
.2实例化中心设备
//options 后代表的是是否开启alert提示
self.CBManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{@"CBCentralManagerOptionShowPowerAlertKey":@NO}];
代理方法实现监控蓝牙开启状态以及连接发送数据
.1实时监控蓝牙打开状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case :
// 第一个参数填nil代表扫描所有蓝牙设备,第二个参数options也可以写nil,调用下方方法后会开始扫描周围蓝牙设备开始调用3.2中代理方法
[self.CBManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:YES]}];
break;
}
}
3.2 开始扫描数据,扫描到对应数据后停止扫描
-(void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
//获取广播数据中的名称
NSString*localName=[advertisementData valueForKey:@"kCBAdvDataLocalName"];
if ([localName hasPrefix:@"HC"]) {
_peripheral = peripheral;
[self.CBManager connectPeripheral:_peripheral options:nil];
// 扫描到设备之后停止扫描
[self.CBManager stopScan];
}
}
.3连接到当前设备
//连接外设成功,开始发现服务
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier);
// 连接设备之后设置蓝牙对象的代理,扫描服务
[self.peripheral setDelegate:self];
[self.peripheral discoverServices:nil];
NSLog(@"扫描服务");
}
3.3 发现服务,然后根据服务查找对应的特征
//已发现服务
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
NSLog(@"发现服务.");
for (CBService *s in peripheral.services) {
if ([s.UUID.UUIDString isEqual:ServicesUUID]) {
[peripheral discoverCharacteristics:nil forService:s];
}
}
}
3.4 根据服务UUID查询对应服务的特征
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *c in service.characteristics) {
if ([c.UUID.UUIDString isEqual:CharacteristicsUUID]) {
self.characteristic = c;
//把数据进行转NSData编码,转数据成功后写数据给蓝牙外设
NSData *data = [self returnDataWithKey:self.BleId];
[_peripheral setNotifyValue:YES forCharacteristic:c];
[_peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse];
}
3.5 读取蓝牙外设返回的数据
//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if ([characteristic.UUID.UUIDString isEqual:CharacteristicsUUID]) {
NSData * data = characteristic.value;
///<d6c8d6c8 40010000 00000000 00000000 00000000> 成功返回数据
///<d6c8d6c8 80010000 00000000 00000000 00000000> 校验和错误返回数据
// 比对data数据是否是返回成功的data数据或者失败的数据 ,成功返回,提示用户开门成功
}
- 遇到的问题:
4.1连接蓝牙设备发送数据,蓝牙设备收不到,具体原因是不清楚需要连接的硬件具体的服务以及服务对应的特征;
4.2 发送数据是需要发送先把当前的16进制数据转byte,然后转NSData类型数据;
4.2.1 获取到门锁上15位ID号,15位ID字符转16进制数据:
- (NSString *)hexStringFromString:(NSString *)string
4.2.2 转换好的15位ID号进行校验和处理,处理后拼接字符串,把拼接好的16进制字符串转byte然后再转NSData类型数据:
- (NSData *)returnDataWithKey:(NSString *)key
4.2.3 NSData数据转NSString类型:
- (NSString *)convertDataToHexStr:(NSData *)data
4.2.4 获取蓝牙外设发送的数据时,每次接收最大为20字节,获取时需要多次获取拼接data,然后再与对应的协议进行对比;
iOS蓝牙交互(4.0)的更多相关文章
- iOS蓝牙开发(4.0)详解
最近由于项目需要, 一直在研究蓝牙4.0,在这儿分享给大家, 望共同进步. 一.关于蓝牙开发的一些重要的理论概念: 1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/Core ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- iOS蓝牙4.0开发
文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...
- https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程
The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- ios 蓝牙相关
ios蓝牙开发项目实战 -(附小米手环实例) 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...
- iOS蓝牙BLE开发
蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...
- H5与安卓、IOS的交互,判断微信、移动设备、安卓、ios
一.通过用户代理可以判断网页当前所在的环境 var browser={ versions:function(){ var u = navigator.userAgent, app = navigato ...
- Unity3D与iOS的交互
1. 关于Unity3D Unity3D(以下简称U3D)是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开 ...
随机推荐
- Scrapy爬取西刺代理ip流程
西刺代理爬虫 1. 新建项目和爬虫 scrapy startproject daili_ips ...... cd daili_ips/ #爬虫名称和domains scrapy genspider ...
- 17.leetcode 237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- list集合为空或为null的区别
简述 判断一个list集合是否为空,我们的惯性思维是判断list是否等于null即可,但是在Java中,list集合为空还是为null,这是两码事. 新建一个list对象,默认值是空,而非null: ...
- html5 textarea 文本框根据输入内容自适应高度
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- NYOJ--95--multiset--众数问题
/* Name: NYOJ--95--众数问题 Date: 20/04/17 16:02 Description: multiset水过 */ #include<set> #include ...
- Red Hat 9.0 Linux 分辨率修改
Red Hat 9.0 Linux 分辨率修改 我是在VMware Workstation中装了一个红帽的Linux系统,装上之后发现分辨率有点低,是800x600的,看着很不舒服,然后就想着怎么样可 ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Python web框架总结
web框架总结 前提 一个web框架需要包含的组件或者功能有: router orm request and response cookies and session template engine ...
- vi替换方法总结
1. 基本的替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivian/sky/ ...
- Matlab学习笔记(1)
在帮助文档中查看学习视频的时候.出现以下对话框,不能查看 解决办法: 在系统环境变量中找到Path.添加“%SystemRoot%\system32;%SystemRoot%;%SystemRoo ...