随着iOS项目开发  很多app需要通过蓝牙与设备连接

蓝牙开发注意:

先定义中心设备和外围设备以及遵守蓝牙协议

@interface ViewController()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (strong, nonatomic) CBCentralManager *manager;
@property (nonatomic, strong) CBPeripheral *peripheral;@property (nonatomic, weak)NSTimer * connentTimer;@end

再实现delegate方法

  1. 判断蓝牙状态,如成功则扫描指定UUID设备(如不指定UUID,则无法后台持续连接)
  2. 当发现指定设备后,连接该设备
  3. 当连接指定外围设备成功,编写定时器,每秒读取1次RSSI
  4. 当监听到失去和外围设备连接,重新建立连接
  5. 当读取到RSSI值,打印出它的值
//蓝牙状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{NSString * state = nil;switch ([central state]){case CBCentralManagerStateUnsupported:state = @"The platform/hardware doesn't support Bluetooth Low Energy.";break;//应用程序没有被授权使用蓝牙case CBCentralManagerStateUnauthorized:state = @"The app is not authorized to use Bluetooth Low Energy.";break;//尚未打开蓝牙case CBCentralManagerStatePoweredOff:state = @"Bluetooth is currently powered off.";break;//连接成功case CBCentralManagerStatePoweredOn:[self.manager scanForPeripheralsWithServices:nil options:nil];state = @"work";break;case CBCentralManagerStateUnknown:default:;}NSLog(@"Central manager state: %@", state);
}
//查找设备
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{//每个蓝牙设备有自己唯一的标识符,根据标识符确认自己要连接的设备if ([peripheral.identifier isEqual:self.peripheral.identifier]){self.peripheral = peripheral;//数据连接定时器self.connentTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(connentPeripheral) userInfo:@"timer" repeats:YES];[self.connentTimer fire];}
}- (void)connentPeripheral {//连接外设self.manager.delegate = self;[self.manager connectPeripheral:_peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];}//连接成功后调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{NSLog(@"Did connect to peripheral: %@,%@", peripheral,peripheral.name);[peripheral setDelegate:self];  //查找服务[peripheral discoverServices:nil];[self.connentTimer invalidate];//监测设备是否断开了
//    [self createWorkDataSourceWithTimeInterval:1];
}
//当监听到失去和外围设备连接,重新建立连接
//这个方法是必须实现的,因为蓝牙会中断连接,正好触发这个方法重建连接。重建连接可能造成数秒后才能读取到RSSI。- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{[self.manager connectPeripheral:peripheral options:nil];
}- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{NSLog(@"%@",error.description);
}//返回的蓝牙服务通知通过代理实现
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{if (error){NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);return;}for (CBService *service in peripheral.services){
//        NSLog(@"Service found with UUID: %@", service.UUID.UUIDString);//发现服务if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]])//heart rate{//在一个服务中寻找特征值[peripheral discoverCharacteristics:nil forService:service];}}
}//返回的蓝牙特征值通知通过代理实现
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{if (error){NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);return;}for (CBCharacteristic * characteristic in service.characteristics){NSLog(@"characteristic:%@",characteristic);if( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]]){[self notification:service.UUID characteristicUUID:characteristic.UUID peripheral:peripheral on:YES];
//            [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];}}
}//处理蓝牙发过来的数据
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{}-(void) notification:(CBUUID *) serviceUUID characteristicUUID:(CBUUID *)characteristicUUID peripheral:(CBPeripheral *)p on:(BOOL)on
{CBService *service = [self getServiceFromUUID:serviceUUID p:p];if (!service){//        if (p.UUID == NULL) return; // zach ios6 addedche//        NSLog(@"Could not find service with UUID on peripheral with UUID \n");return;}CBCharacteristic *characteristic = [self getCharacteristicFromUUID:characteristicUUID service:service];if (!characteristic){//        if (p.UUID == NULL) return; // zach ios6 added//        NSLog(@"Could not find characteristic with UUID  on service with UUID  on peripheral with UUID\n");return;}[p setNotifyValue:on forCharacteristic:characteristic];}-(CBService *) getServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)p
{for (CBService* s in p.services){if ([s.UUID isEqual:UUID]) return s;}return nil; //Service not found on this peripheral
}
-(CBCharacteristic *) getCharacteristicFromUUID:(CBUUID *)UUID service:(CBService*)service {for (CBCharacteristic* c in service.characteristics){if ([c.UUID isEqual:UUID]) return c;}return nil; //Characteristic not found on this service
}

iOS蓝牙开发数据实时传输相关推荐

  1. iOS蓝牙开发---CoreBluetooth[BLE 4.0] 初级篇[内附Demo地址]

    一.蓝牙基础知识 (一)常见简称 1.MFI  make for ipad ,iphone, itouch 专们为苹果设备制作的设备,开发使用ExternalAccessory 框架(认证流程貌似挺复 ...

  2. iOS 蓝牙开发资料记录

    一.蓝牙基础认识:   1.iOS蓝牙开发:   iOS蓝牙开发:蓝牙连接和数据读写   iOS蓝牙后台运行  iOS关于app连接已配对设备的问题(ancs协议的锅)          iOS蓝牙空 ...

  3. iOS 蓝牙开发 BabyBluetooth蓝牙库介绍

    BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容iOS和Mac OS X. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮 ...

  4. iOS蓝牙开发:蓝牙连接和数据读写

    当下蓝牙开发可谓是越来越火,不论是智能穿戴的兴起还是蓝牙家具,车联网蓝牙等等,很多同学也会接触到蓝牙的项目,我从事蓝牙开发也有一段时间了,经手了两个项目.废话不多说了,先向大家简单的介绍有关蓝牙开发的 ...

  5. iOS蓝牙开发:蓝牙的连接和数据的读写

    蓝牙开发说简单也简单,说不简单也有点难,开发人员在首次开发蓝牙前首先需要搞清楚蓝牙开发的概念,还要了解掌握蓝牙开发的一整套流程,这样才能快速上手开发蓝牙. 蓝牙开发分为两种模式:管理者模式和中心者模式 ...

  6. ios 蓝牙开发总结

    随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的发展史也不难看出 ...

  7. iOS 蓝牙开发和注意点

    前言 蓝牙传输所用的框架是<CoreBluetooth/CoreBluetooth.h> 蓝牙连接需要中心管理者和外部设备,我们所做的开发基本是围绕中心管理来的: 蓝牙设备发过来的每个数据 ...

  8. iOS蓝牙开发(三):iOS中蓝牙模块OTA升级(YModem协议)

    上一篇简单介绍了蓝牙4.0的iOS实现代码,详细的东西大家可以去github上搜babyBluetooth,里面有一些学习资料,接下来分享的是OTA升级的东西,我们假定看这篇文章的时候,关于iOS和外 ...

  9. ios蓝牙开发 ------ CoreBluetooth 教程lt;转gt;

    原文地址:http://blog.csdn.net/jimoduwu/article/details/8917104 去App Store搜索并下载"LightBlue"这个App ...

最新文章

  1. 欢迎大家批评:CSDN Blog用户体验调查
  2. Java学习笔记(十一)--类与对象
  3. Visual Studio 2015 前瞻 属性初始化赋值!
  4. 计算机启动完成后操作系统负责管理的是,终极:如果计算机启动后无法进入系统,旧驱动程序会教您如何处理...
  5. strcasecmp()--忽略大小写比较字符串
  6. 基于TCP协议的网络摄像头的设计与实现
  7. 为何 iOS 越来越偏爱 Swift?
  8. 5G时代下的移动边缘计算(MEC)探索系列之四
  9. “什么?中东的土豪给我们 App 充了大钱!”
  10. 音视频播放器—初始化操作
  11. Ubuntu 解压缩7z文件
  12. python图像缺陷检测_python OpenCV 实现缺陷检测
  13. linux 将当前时间往后调整2分钟_linux调整系统时间 永久 z | 学步园
  14. python自动化ppt_python自动化办公手册之python操作PPT
  15. LeetCode常用算法模式大厂面试题整理
  16. python 模块paramiko
  17. csp试题2:公共钥匙盒
  18. .dwg转换为.svg
  19. rx560d linux 图形设计,关系steam linux版游戏 使用体会和个人建议
  20. 【MMPose】在HRNet应用SimDR(SimCC)/Part.3-处理头篇(Head)

热门文章

  1. 理解RPC和LPC的概念
  2. android 粘性弹窗,android组件化通信之EventBus粘性事件解析
  3. c++11 获取时间戳
  4. 网页版阴阳师与服务器断开、,阴阳师服务器崩了怎么回事?阴阳师服务器崩了怎么解决...
  5. Java——自定义异常类
  6. Wine开发系列之——入门
  7. 视频编辑:VisioForge Video Edit SDK .Net 15.5 标准版 Crack
  8. Zoom商务版和zoom专业版,你怎么选?
  9. SSM框架搭建的步骤
  10. python3——matplotlib绘图2