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 ...
随机推荐
- chrome实现全浏览器跨域ajax请求
如图,在chrome快捷方式上打开属性栏,在‘目标’栏加上后缀--disable-web-security --user-data-dir.即可实现在此浏览器上所有网页的跨域请求.
- Generative Learning algorithms
"generative algorithm models how the data was generated in order to categorize a signal. It ask ...
- [原创]obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用
原文链接:obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用 我们在第16和第17篇中分别介绍了obj-c的KVC与KVO特性,当时举的例子比较fun,太抽象,貌似和实际不沾边哦.那么 ...
- cocos2d-x -------之笔记篇 动画的实现
cocos2d-x 动画的实现 一.实现原理 动画的实现其实就是使用一个完整的动作图片集来实现动画,达到动态的效果 动画动作类(CCAnimate)是加载一个动画类来实现动作. 动画类(CCAnima ...
- rational rose 2003安装及破解
rational rose作为面向对象的统一建模语言的可视化建模工具,包括了统一建模语言(UML),OOSE,以及OMT,可用于可视化建模和公司级水平软件应用的组件构造:此次小编将讲解如何安装及破解r ...
- Make Hadoop 1.2.1 run, my first try
经历两天努力,8月25日下午2点40分,终于让hadoop1.2.1跑起来. 用的是<Hadoop实战第2版>(陆嘉恒)里面的WordCount例子,虽然书是2013年出的,但用的例子还是 ...
- lodash中_.set的用法
_.set(object, path, value) # Ⓢ Ⓣ Ⓝ 设置对象的路径上的属性值.如果路径不存在,则创建它. 参数 1.object (Object): 待扩大的对象. 2.path ( ...
- Android getReadableDatabase() 和 getWritableDatabase()
Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...
- HTML之学习笔记(六)添加链接
html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签 <a href="URL">热点文字 ...
- C# 第三方控件 错误 LC-1
删掉项目下面的Properties\licenses.licx 文件