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开发
文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...
- iOS 蓝牙4.0开发
背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...
- iOS蓝牙4.0开发(BLE)
智能设备 和 app 通过 BLE通讯的两种模型 模型一:设备提供数据,app 展示数据: 比如小米手环 模型二:app提供数据,设备接收: 模型与corebluetooth的对应关系: 模型一:智能 ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- 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
iOS的蓝牙用到了 CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...
- android 蓝牙4.0 开发介绍
最近一直在研究一个蓝牙功能 由于本人是菜鸟 学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描 ...
- CoreBluetooth——IOS蓝牙4.0使用心得
原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...
随机推荐
- jqGrid源代码分析(一)
废话少说.先上grid.base.js 整体结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3B5MTk4ODEyMDE=/font/5a6L5L2 ...
- html_day2
总结下今天学的HTML知识.单词 跑马灯标记 <marquee></marquee>属性: direction:滚动的方向 取值:left .right. up. down b ...
- ORACLE SEQUENCE用法 (自增长)
在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence 首先要有CREATE SEQUENCE或者CREATE ...
- 如何循序渐进有效学习 JavaScript?
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:宋学彦链接:http://www.zhihu.com/question/19713563/answer/23068003来源: ...
- struts2 注解方式
struts2扫描方法: 扫描其位于包的命名注解的类 “struts, struts2, action 或 actions“. 接着,扫描相匹配下列任一条件的文件: 实例了 com.opensymph ...
- Java IO4:字符流进阶及BufferedWriter、BufferedReader
字符流和字节流的区别 拿一下上一篇文章的例子: 1 public static void main(String[] args) throws Exception 2 { 3 File file = ...
- 安装ubuntu14.10系统的那些瞎折腾
前段时间自作孽,安装了ubuntu14.04的64位系统,而我的笔记本又是那种老古董,2G的内存所以装好之后各种不稳定,索性这个周末就重装一下吧,本来打算是直接装我以前的那个ubuntu12.04-i ...
- [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释
getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...
- python笔记之hashlib模块
涉及加密服务:14. Cryptographic Services其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA256.SHA384.SHA ...
- Mac OSX下面的博客客户端Marsedit使用
在windows下面,有一个很好用的博客客户端,叫做windows live writer,不得不感叹,其所见即所得的方面真的是很方便,特别是还可以方便的把word上的内容直接帖上去,包括文件中 ...