iOS蓝牙传输数据演示-3
蓝牙传输数据演示
在上一小节中,我们一起开发了基于蓝牙通讯的工具类,该类中详细的实现蓝牙连接流程中的每一个环节
本小节我们就以给小米手环发送数据使其震动来演示我们工具类的用法
工具类本身具有通用性,属于MVC中的
M层,只负责处理自身负责的处理,不处理任何的业务逻辑和UI我的小米手环的identifer:60C955B2-8F7C……
- 后面我就不写了,每一个手环的唯一标识符都是不一样的
- 能够让小米手环震动的特征的UUID:2A06
能够让小米手环震动的数据:2(二进制数据)
示例效果:1。点击开始扫描按钮,搜索蓝牙设备,并且将外设的信息显示在tableview中 2.点击指定的tableviewcell,让小米手环震动
#import "ViewController.h" #import "HMBluetoothManager.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView; @end //我的小米手环dentifier
#define kIdentifier @"60C955B2-8F7C-8784-665F-D05E520F5A12" @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
} #pragma mark -扫描按钮
- (IBAction)scanButtonClick:(id)sender { kHMBluetoothManager.UUID = @"2A06";
//1.开始扫描
[kHMBluetoothManager BeginScanPeripheral:^(CBPeripheral *peripheral) {
//刷新tableview
[self.tableView reloadData]; }];
} #pragma mark -tableviewdelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return kHMBluetoothManager.scanArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; CBPeripheral *peripheral = kHMBluetoothManager.scanArr[indexPath.row]; cell.textLabel.text = [peripheral.identifier UUIDString]; cell.detailTextLabel.text = peripheral.name; return cell;
} //点击cell连接设备
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取点击的外设
CBPeripheral *peripheral = kHMBluetoothManager.scanArr[indexPath.row]; //判断是否是我的小米手环(专门为了测试买的),因为蓝牙会扫描到周边很多外设,我们通过唯一标识符来判断自己的外设
if ([[peripheral.identifier UUIDString] isEqualToString:kIdentifier]) { //3.连接设备
[kHMBluetoothManager connectPeripheral:peripheral Completion:^(CBPeripheral *peripheral, NSString *connectState) {
NSLog(@"%@",connectState);
//4.发送数据
//实际开发中,扫描特征会有一定的延迟,我们可以通过回调或者通知来获取发现特征的回调,这里为了快速演示,我就设置了3s的延迟
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Byte *byte[];
byte[]= & 0xff;
NSData *data = [NSData dataWithBytes:byte length:];
[kHMBluetoothManager writeValue:data toPeripheral:kHMBluetoothManager.currentPeripheral characteristic:kHMBluetoothManager.currentCharacteristic];
}); }];
}
else
{
NSLog(@"这不是你的小米手环");
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
iOS蓝牙传输数据演示-3的更多相关文章
- iOS蓝牙BLE开发
蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...
- iOS 蓝牙的GameKit用法
一.连接蓝牙 显示可以连接的蓝牙设备列表 - (IBAction)buildConnect:(id)sender { // 创建弹窗 GKPeerPickerController *ppc = [[G ...
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- 蓝牙(3)如何通过蓝牙传输数据及UUID详介
如何通过蓝牙传输数据 通过蓝牙传输数据与Socket类似.在网络中使用Socket和ServerSocket控制客户端和服务端的数据读写.而蓝牙通讯也由客户端和服务端Socket来完成.蓝牙客户端So ...
- 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 蓝牙开发资料记录
一.蓝牙基础认识: 1.iOS蓝牙开发: iOS蓝牙开发:蓝牙连接和数据读写 iOS蓝牙后台运行 iOS关于app连接已配对设备的问题(ancs协议的锅) iOS蓝牙空中 ...
- iOS蓝牙APP常驻后台
iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为: 1.设置plist,蓝牙权限 2.到target-capabilities-backgr ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- iOS蓝牙原生封装,助力智能硬件开发
代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...
随机推荐
- Parallel Tests
Parallel Tests Parallel Android Tests Appium provides a way for users to automate multiple Android s ...
- atol实现【转】
int my_atoi(const char *str) { assert(str != NULL); ; ; while(*str == ' ' || *str == '\n' || *str == ...
- HDU4850 Wow! Such String! —— 字符串构造
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4850 代码如下: #include <iostream> #include <cst ...
- 'gbk' codec can't encode character '\xa5' in position 4546: illegal multibyte sequence错误解决
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030') 原文 http://blog.csdn.net/jim7424 ...
- NSMutableURLRequest,在POST方式下传递参数
1. [代码][C/C++]代码 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSUs ...
- 页面渲染——页面合成(composition)的优化
合成(composition)意味着将网页中已经绘画好的部分结合在一起,且展示在屏幕上. 坚持使用transform和opacity属性来操作你的动画animation 在有动画的元素上使用 will ...
- 使用cygwin注意事项二
使用cygwin时,一定要区分当前运行的是cygwin下的进程还是windows下的进程,如:使用vim, 假如cygwin下没安装vim, windows下安装了,那么你运行的就是windows下的 ...
- C语言之fileno()函数--获取已经打开的文件的文件描述符(小技巧)
open函数相关的: /* open 是系统调用 返回的是文件句柄*/ #include <sys/stat.h> #include <fcntl.h> int open(c ...
- Ubuntu 安装 texlive
下载网站: http://tug.org/texlive/acquire-netinstall.html 此处解释texlive配置PATH gedit ~/.bashrc 在文件最后添加以下内容, ...
- python学习笔记——Thread常用方法
Thread对象中的一些方法: 以前说过多线程,用到threading模块中的Thread对象,其中的start和run方法比较熟悉了,start()是重载了Thread对象中的run方法,其实作用还 ...