iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点
1.首先建立这个三个参数
@property (nonatomic,strong)CBCentralManager * manager;
@property (nonatomic,strong)CBPeripheral * peripheral; @property (nonatomic,strong)CBCharacteristic *writeDataCharacteristic
2、初始化CBCentralManager
-(CBCentralManager *)manager
{if (!_manager ){_manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil]; } return _manager; }
3.开始扫描蓝牙
[self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}];
4.当蓝牙状态改变的时候就会调用这个方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{switch (central.state) {case CBCentralManagerStateUnknown://设备不支持的状态 NSLog(@">>>CBCentralManagerStateUnknown"); { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"设备不支持"); }); } break; case CBCentralManagerStateResetting: //正在重置状态 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"正在重置状态"); }) ; } NSLog(@">>>CBCentralManagerStateResetting"); break; case CBCentralManagerStateUnsupported: //设备不支持的状态 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"设备不支持的状态"); }); } NSLog(@">>>CBCentralManagerStateUnsupported"); break; case CBCentralManagerStateUnauthorized: // 设备未授权状态 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"设备未授权状态"); }); } NSLog(@">>>CBCentralManagerStateUnauthorized"); break; case CBCentralManagerStatePoweredOff: //设备关闭状态 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"设备关闭状态"); }); } NSLog(@">>>CBCentralManagerStatePoweredOff"); break; case CBCentralManagerStatePoweredOn: NSLog(@">>>CBCentralManagerStatePoweredOn"); //开始扫描周围的外设 [self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}]; break; default: break; } }
5.连接外围设备
- (void)connect:(CBPeripheral *)peripheral
{// 连接外围设备[self.manager connectPeripheral:peripheral options:nil]; }
6.连接玩设备就调用成功的方法,然后开始扫描
//连接到Peripherals-成功 //扫描外设中的服务和特征 连接上外围设备的时候会调用该方法
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name); //设置的peripheral委托CBPeripheralDelegate //@interface ViewController : UIViewController [peripheral setDelegate:self]; //扫描外设Services,成功后会进入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ [peripheral discoverServices:nil]; } //连接到Peripherals-失败 -(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheral name],[error localizedDescription]); }
7.发现服务和设备的服务设备services
//发现服务
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{NSLog(@"Discovered services for %@ ", peripheral.name); if (![self.dataSourceArray containsObject:peripheral]) { [self.dataSourceArray addObject:peripheral]; NSLog(@"%@",peripheral); dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); } } /** * 发现外围设备的服务会来到该方法(扫描到服务之后直接添加peripheral的services) */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"发现外围设备的服务"); for (CBService *serivce in peripheral.services) { NSLog(@"====%@------%@+++++++",serivce.UUID.UUIDString,self.peripheral.identifier); if ([serivce.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]) { // characteristicUUIDs : 可以指定想要扫描的特征(传nil,扫描所有的特征) [peripheral discoverCharacteristics:nil forService:serivce]; } } }
8.找到了设备的服务然后扫描特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{NSLog(@"发现外围设备的特征");for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"====%@------+",characteristic.UUID.UUIDString); if ([characteristic.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_TRANS_TX]) { // 拿到特征,和外围设备进行交互 [self notifyCharacteristic:peripheral characteristic:characteristic]; } } for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"====%@------+",characteristic.UUID.UUIDString); if ([characteristic.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_TRANS_RX]) { // 拿到特征,和外围设备进行交互 保存写的特征 self.writeDataCharacteristic = characteristic; } } }
9.最后一步要写上通知这个一定要写上
//设置通知
-(void)notifyCharacteristic:(CBPeripheral *)peripheralcharacteristic:(CBCharacteristic *)characteristic{//设置通知,数据通知会进入:didUpdateValueForCharacteristic方法[peripheral setNotifyValue:YES forCharacteristic:characteristic];[self.manager stopScan]; } //取消通知 -(void)cancelNotifyCharacteristic:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic{ [peripheral setNotifyValue:NO forCharacteristic:characteristic]; }
10.大功告成只差一步就是写数据
//写数据
-(void)writeCharacteristic:(CBPeripheral *)peripheralcharacteristic:(CBCharacteristic *)characteristicvalue:(NSData *)value
{//只有 characteristic.properties 有write的权限才可以写 if(characteristic.properties & CBCharacteristicPropertyWrite){ /* 最好一个type参数可以为CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,区别是是否会有反馈 */ [peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; }else{ NSLog(@"该字段不可写!"); } }
11.希望能帮到人,让别人少走点坑。
转载于:https://www.cnblogs.com/yuejunjie/p/5282321.html
iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点相关推荐
- iOS开发之蓝牙4.0技术完美实现
CSDN博客 BaiHuaXiu123 博客专家 iOS开发之蓝牙4.0技术完美实现 发表于2016/5/1 21:13:06 8034人阅读 前言 前端时间,同学在做项目过程中遇到 ...
- iOS开发之蓝牙4.0开发使用
转载自https://www.jianshu.com/p/f0e4b0e98336 2015年的时候自己写过一个蓝牙4.0项目,当忘了写博客,最近看到这篇博客写的挺详细,所以转载一下 一: 介绍 蓝牙 ...
- iOS开发 BLE 蓝牙4.0
2019独角兽企业重金招聘Python工程师标准>>> 导入框架 #import "ViewController.h" #import <GameKit/G ...
- iOS开发之蓝牙/Socket链接小票打印机
原文链接:https://www.jianshu.com/p/3f36ae67f5a4 來源:简书 本文转载至简书的一位作者,如有侵权,请及时告知 前言 之前公司有个面向商户的项目,需要连接商户打印机 ...
- iOS开发之蓝牙通信
一.引言 蓝牙是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单.相关的蓝牙操作由专门的 CoreBluetooth.framework进行统一管理.通过蓝牙进 ...
- 【iOS开发】swift 3.0 延长设置launch image启动页面图片显示时间
在ViewController.swift里面的viewDidLoad方法中添加代码(3.0为秒数,可自定义): override func viewDidLoad() {super.viewDidL ...
- 【零基础学习iOS开发】【02-C语言】10-函数
一.基本概念 1.什么是函数 不论什么一个C语言程序都是由一个或者多个程序段(小程序)构成的.每一个程序段都有自己的功能,我们一般称这些程序段为"函数". 所以.你能够说C语言程序 ...
- iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习
iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习 如果你不希望应用运行时 iPhone 进入锁屏待机状态,加入下面这行代码即可 [[UIApplication share ...
- iOS开发的架构模式
iOS开发的架构模式 0.VIPER 为了减轻Controller层负担的方法,而VIPER架构其实是将Controller再细分成三层,分别是View.Interactor.Presenter,已达 ...
最新文章
- GoogleNet是怎么理解图像的?谷歌大神教你读懂「神经特征可视化」
- Spring3.2.4集成quartz2.2.1定时任务(demo).
- directoryinfo 读取 映射磁盘_LoaRunner性能测试系统学习教程:磁盘监控(5)
- python input 文件名_Python播放音频与录音
- 使用Hyper-V Server创建Linux虚拟机
- Netty工作笔记0007---NIO的三大核心组件关系
- .Net 中的序列化与反序列化[概述]
- 新手学习Linux之grep
- Voxel-Based Global Illumination
- Java程序性能优化
- 将p12证书导入java
- 贝叶斯分析好坏_交易必读|浅谈贝叶斯分析
- 浅谈Foxmail邮件迁移
- 高等数学笔记:留数法
- zabbix下载方式
- Inspector检视视图
- firefox调试html5程序,用 Firefox 开发者工具调试现代 Web 应用程序
- VBA多条EXCEL记录写入到WORD文档中
- 小白终是踏上了这条不归路----小文的mysql学习笔记目录
- ubuntu18.04 分辨率设置
热门文章
- 高可用与负载均衡(7)之聊聊Lvs-DR+Keepalived的解决方案
- 深入了解asp.net框架。生命周期以及事件处理机制
- [设计模式] 8 组合模式 Composite
- RedOffice教你DIY环保小日历
- C#_获取文件路径中的文件名_扩展名
- buildroot 问题
- 浅析epoll – epoll函数深入讲解
- e2fsprogs制作嵌入式 mkfs.ext2 mkfs.ext3 mkfs.ext4
- uboot 命令分析(一) — bootm
- 第五章 云原生与容器技术