CoreBluetooth——IOS蓝牙4.0使用心得
文章出处http://blog.csdn.net/xgcyangguang
所做的东西是通过手机/pad与蓝牙4.0的设备进行连接,之后设备上按对应的按键我们会收到对应的数值。首先需要和做蓝牙4.0的同事沟通好通信协议,具体的数据解析部分就不过多赘述了,主要写一下蓝牙接收数据的部分
1.和做蓝牙的同事沟通好设备的UUID以及特点,可以把他们写成宏
#define TRANSFER_SERVICE_UUID @"0000fff0-0000-1000-8000-00805f9b34fb" #define TRANSFER_CHARACTERISTIC_UUID @"0000fff7-0000-1000-8000-00805f9b34fb"
2.在.H文件中导入两个头文件,并在接口中实现两个协议
#import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> #import "UUID.h" //需要实现协议 @interface ViewController () < CBCentralManagerDelegate, CBPeripheralDelegate> { }
#pragma mark 蓝牙设备 @property (strong, nonatomic) CBCentralManager *centralManager; //接收 @property (strong, nonatomic) CBPeripheral *discoveredPeripheral; //外设 @end
4.开始蓝牙配置
#pragma mark 在初始化界面结束后设置自己为代理 - (void)viewDidLoad { [super viewDidLoad]; _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } #pragma mark 此处监控一下中央的状态值,可以判断蓝牙是否开启/可用 - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSMutableString *stringForCentralManagerState = [NSMutableString stringWithString:@"UpdateState:"]; switch (central.state) { case CBCentralManagerStateUnknown: [stringForCentralManagerState appendString:@"Unkown\n"]; break; case CBCentralManagerStateUnsupported: [stringForCentralManagerState appendString:@"Unsupported\n"]; case CBCentralManagerStateUnauthorized: [stringForCentralManagerState appendString:@"Unauthorized\n"]; case CBCentralManagerStateResetting: [stringForCentralManagerState appendString:@"Resetting\n"]; case CBCentralManagerStatePoweredOff: [stringForCentralManagerState appendString:@"PowerOff\n"]; case CBCentralManagerStatePoweredOn: //设备支持BLE并且可用 [stringForCentralManagerState appendString:@"PoweredOn\n"]; //开始搜索 [self scan]; break; default: [stringForCentralManagerState appendString:@"none\n"]; break; } NSLog(@"%@", stringForCentralManagerState); } #pragma mark 扫描 - (void)scan { //第一个参数如果设置为nil,会寻找所有service [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }]; NSLog(@"Scanning started"); } #pragma mark 发现设备,连接 //一旦符合要求的设备被发现,就会回调此方法 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"Discovered %@ at %@", peripheral.name, RSSI); if (self.discoveredPeripheral != peripheral) { self.discoveredPeripheral = peripheral; // 连接 NSLog(@"Connecting to peripheral %@", peripheral); [self.centralManager connectPeripheral:peripheral options:nil]; } } #pragma mark 未能连接的处理方法 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"Failed to connect to %@. (%@)", peripheral, [error localizedDescription]); // [self cleanup]; } #pragma mark 当连接上设备 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Peripheral Connected"); // 已连接上设备,故停止搜索 [self.centralManager stopScan]; NSLog(@"Scanning stopped"); // Make sure we get the discovery callbacks peripheral.delegate = self; // 寻找指定UUID的Service [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]]; } #pragma mark 发现设备上指定Service会回调此处 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"Error discovering services: %@", [error localizedDescription]); return; } // 寻找指定UUID的Characteristic for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service]; } } #pragma mark 找到指定UUID的Characteristic会回调此处 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"Error discovering characteristics: %@", [error localizedDescription]); return; } for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) { NSLog(@"find the characteristic"); [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } } } - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error discovering characteristics: %@", [error localizedDescription]); return; } NSLog(@"value --> %@",characteristic.value); #pragma mark 把接收到的数据进行截取 //此处我们就可以拿到value值对其进行数据解析了 NSData *data = characteristic.value; }
#pragma mark 蓝牙断开后自动重连 -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"Peripheral Disconnected"); self.discoveredPeripheral = nil; [self scan]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"蓝牙连接状态" message:@"连接已断开" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"关闭", nil]; [alert show]; }
5.蓝牙后台运行
CoreBluetooth——IOS蓝牙4.0使用心得的更多相关文章
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- iOS蓝牙4.0开发
文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...
- [iOS 基于CoreBluetooth的蓝牙4.0通讯]
一.首先大致介绍下蓝牙4.0的模式,中心和周边: 一般情况下,iPhone作为中心,接收来自周边传感器(比如手环等)采集的数据. 二.那整一个数据通讯的协议是怎样的呢? 为什么要一层层搞这么复杂呢?据 ...
- iOS蓝牙4.0
iOS的蓝牙用到了 CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...
- iOS 蓝牙4.0开发
背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...
- iOS蓝牙4.0协议简单介绍
iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...
- 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蓝牙4.0开发(BLE)
智能设备 和 app 通过 BLE通讯的两种模型 模型一:设备提供数据,app 展示数据: 比如小米手环 模型二:app提供数据,设备接收: 模型与corebluetooth的对应关系: 模型一:智能 ...
- iOS蓝牙4.0开发例子
1建立中心角色 1 2 3 #import <CoreBluetooth/CoreBluetooth.h> CBCentralManager *manager; manager = [ ...
随机推荐
- 见证历史 -- 2013 NBA 热火夺冠之路有感
见证历史-- 2013 NBA 热火夺冠之路有感今年NBA季后赛从第一轮看起,到最终的热火夺冠,应该看得是最爽的一次.但一些情节和细节,回忆起来,深有感悟. 1. 做人要低调詹宁斯豪言演黑八雄鹿本赛季 ...
- Memcache知识点梳理
Memcache知识点梳理 Memcached概念: Memcached是一个免费开源的,高性能的,具有分布式对象的缓存系统,它可以用来保存一些经常存取的对象或数据,保存的数据像一张巨大的HAS ...
- RFID应用范围
RFID应用范围 (1)物流: 物流过程中的货物追踪,信息自动采集,仓储应用,港口应用,邮政,快递 (2)零售: 商品的销售数据实时统计,补货,防盗 (3)制造业: 生产数据的实时监控,质量追踪,自动 ...
- InnoDB的WAL方式学习
之前写过一篇博文,<不好的MySQL过程编写习惯>(http://www.cnblogs.com/wingsless/p/5041838.html).这篇博文里强调了不要循环的提交事务,尽 ...
- jasperreports+iReport制作报表笔记
一. 准备工作 1. 下载相关工具:点击此下载相关工具,jasperreports-5.6.0-project.zip中有提供所需的jar包. 二. 安装ant 1. 解压apache-ant-1.9 ...
- Java的SPI机制与简单的示例
一.SPI机制 这里先说下SPI的一个概念,SPI英文为Service Provider Interface单从字面可以理解为Service提供者接口,正如从SPI的名字去理解SPI就是Service ...
- iOS UIScrollView 无法滚动 没有弹簧效果解决方案
一般情况下,检查如下: 1.没有设置contentSize或者contentSize的尺寸小于等于该scrollView的尺寸 2.scrollView.enable = NO;(仅仅是让scroll ...
- nc分析代理服务器
最近洒家搞了一个代理服务器(参见 折腾阿里云笔记 ).以前ƒq的时候代理服务器都是开在127.0.0.1,浏览器到代理服务器之间不会有中间人监听问题.现在代理服务器不在本机,洒家就突然想到了这些问题: ...
- linux进程间通信-消息队列
一 消息队列的介绍 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法. 每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构. 我们可以通过发送消息来避免命名管道的 ...
- [麦先生]初学Laravel框架与ThinkPHP框架的不同(2)
在经过了一段时间的开发后,我对Laravel框架的认识又在逐步的加深,对于这个世界占有量NO.1的框架... 我想说,我已经在逐步的感受到他的恐怖之处... 一.建表--Laravel在数据库建表上 ...