IOS学习之蓝牙4.0
1建立中心角色
|
1
2
3
|
#import <CoreBluetooth/CoreBluetooth.h> CBCentralManager *manager; manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; |
2扫描外设(discover)
[manager scanForPeripheralsWithServices:nil options:options];
3连接外设(connect)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { if([peripheral.name isEqualToString:BLE_SERVICE_NAME]){ [self connect:peripheral]; }s); } -(BOOL)connect:(CBPeripheral *)peripheral{ self.manager.delegate = self; [self.manager connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];} |
4扫描外设中的服务和特征(discover)
|
1
2
3
4
5
6
7
8
9
10
11
|
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Did connect to peripheral: %@", peripheral); _testPeripheral = peripheral; [peripheral setDelegate:self]; <br>//查找服务 [peripheral discoverServices:nil]; } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"didDiscoverServices"); if (error) { NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)]) [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil]; return; } for (CBService *service in peripheral.services) { //发现服务 if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]]) { NSLog(@"Service found with UUID: %@", service.UUID); <br>//查找特征 [peripheral discoverCharacteristics:nil forService:service]; break; } } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if (error) { NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]); [self error]; return; } NSLog(@"服务:%@",service.UUID); for (CBCharacteristic *characteristic in service.characteristics) { //发现特征 if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) { NSLog(@"监听:%@",characteristic);<br>//监听特征 [self.peripheral setNotifyValue:YES forCharacteristic:characteristic]; } }} |
5与外设做数据交互(读 与 写)
读
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ if (error) { NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]); self.error_b = BluetoothError_System; [self error]; return; } // NSLog(@"收到的数据:%@",characteristic.value); [self decodeData:characteristic.value];} |
写
|
1
2
|
NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"]; [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse]; |
IOS学习之蓝牙4.0的更多相关文章
- IOS学习之蓝牙4.0 BLE
IOS学习也一段时间了,该上点干货了.前段时间研究了一下IOS蓝牙通讯相关的东西,把研究的一个成果给大家分享一下. 一 项目背景 简单介绍一下做的东西,设备是一个金融刷卡器,通过蓝牙与iphone手机 ...
- iOS学习之Objective-C 2.0 运行时系统编程
0 导言 本主主要内容包括: 1.概述2.参考3.运行时系统的版本和平台4.和运行时系统的交互5.消息6.动态方法解析7.消息转发8.类型编码9.属性声明 1 概述 Objective-C语言将决定尽 ...
- 蓝牙4.0(包含BLE)简介
1. BLE (低功耗蓝牙)简介 国际蓝牙联盟( BT-SIG,TI 是 企业成员之一)通过的一个标准蓝牙无线协议. 主要的新特性是在蓝牙标准版本上添加了4.0 蓝牙规范 (2010 年6 月 ...
- 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蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- IOS BLE蓝牙4.0
前言: 自己做的项目里面有这么一个功能,总结归纳一下. 先导入必要的框架 CoreBluetooth.framework 在要用到蓝牙的文件里面导入以下头文件 #import <CoreBlu ...
- iOS蓝牙4.0
iOS的蓝牙用到了 CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...
- 【原】iOS学习之第三方-AFNetworking1.3.0
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 AFNetworking 第三方集成到工程中,具体请看上篇博客iOS学习46之第三方CocoaPods的安装和使用(通用方 ...
- iOS 蓝牙4.0开发
背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...
随机推荐
- C#文本文件或其他文件新内容追加
以txt文本为例,以下代码实现a.txt文件中追加内容 FileStream mystream = new FileStream("C:\\a.txt", FileMode.Ope ...
- AutoIt 函数学习之----WinWaitActive
WinWaitActive函数 暂停脚本的执行直至指定窗口被激活(成为活动状态)为止. WinWaitActive ( "窗口标题"[, "窗口文本"[, 超时 ...
- C++ 下 typeof 的实现
现在我们有这样一坨代码: std::vector<int> arr; // ... for(std::vector<int>::iterator iter = arr.begi ...
- BadgeView使用介绍
前段时间做的一个淘宝客的项目,需要在商品图片上添加价格标签,之前自己使用TextView和Cavas绘制的感觉效果一般,今天偶然在CSDN上发现BadgeView这个开源项目,在git下载下来之后,使 ...
- Robot Framework作者建议如何选择自动化测试框架
本文摘自:InfoQ中文站http://www.infoq.com/cn/news/2012/06/robot-author-suggest-autotest Robot Framework作者建议如 ...
- 10.30 NFLS-NOIP模拟赛 解题报告
总结:今天去了NOIP模拟赛,其实是几道USACO的经典的题目,第一题和最后一题都有思路,第二题是我一开始写了个spfa,写了一半中途发现应该是矩阵乘法,然后没做完,然后就没有然后了!第二题的暴力都没 ...
- 去英国Savile Row 做件私人定制手工西装_GQ男士网
去英国Savile Row 做件私人定制手工西装_GQ男士网 去英国Savile Row 做件私人定制手工西装
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- hadoop技术基本架构
一.Hadoop概述 hadoop由两部分组成.各自是分布式文件系统和分布式计算框架MapReduce.当中.分布式文件系统主要用于大规模数据的分布式存储.而MapReduce 则构建在分布式文件系 ...
- server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
宕机监控报警程序 一. 需求来源 宕机对运维人员来说,最痛苦了.怎样检測一台server是否还在正常执行,假设该server宕机,怎样在第一时间监測到并通知一线运维人员进行维护,最大化降低损失. ...