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#access数据库操作
比较凌乱,有时间在整理吧. Provider=Microsoft.Jet.OLEDB.!" private void GetCon() { string strConnection=&quo ...
- asp.net mvc 注册中的邮箱激活功能实现(二)
邮件发送功能封装 /// <summary> /// 发送注册邮件 /// </summary> /// <param name ...
- 什么是位、字节、字、KB、MB
什么是位.字节.字.KB.MB 位:"位(bit)"是电子计算机中最小的数据单位.每一位的状态只能是0或1. 字节:8个二进制位构成1个"字节(Byte)",它 ...
- JS中的prototype属性
JavaScript是基于对象的,任何元素都可以看成对象.然而,类型和对象是不同的.本文中,我们除了讨论类型和对象的一些特点之外,更重要的 是研究 如何写出好的并且利于重用的类型.毕竟,JavaSc ...
- 电池和Adapter切换电路改进实验
目的:很多单电池的机器在大负载的情况下,如把背光开到最亮,运行3D游戏,此时拔DC电源很容易出现机器死机,或花屏现象: 原因:Q5的导通时间不够,希望通过G极的快速放电,加快到导通时间: 修改前的电路 ...
- vs指定QT的工作目录(其它项目也是如此)
当一个工程依赖第三方动态库时,这时vs编译出来后,运行会提示缺少动态库.解决方法: 项目->属性->调试: 工作目录:指定程序运行时的目录 环境:指定程序运行时的环境变量 我们可以在环境变 ...
- Codeforces 263E
Codeforces 263E 原题 题目描述:一个\(n \times m\)的矩阵,每格有一个数,给出一个整数\(k\),定义函数\(f(x, y)\): \[f(x, y)=\sum_{i=1} ...
- POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...
- DKNY_百度百科
DKNY_百度百科 DKNY
- android UI-EditText的长度监听慎用TextWatcher
在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...