一 蓝牙介绍
随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低。从iOS的发展史也不难看出苹果目前对蓝牙技术也是越来越关注,例如苹果于2013年9月发布的iOS7就配备了iBeacon技术,这项技术完全基于蓝牙传输。但是众所周知苹果的设备对于权限要求也是比较高的,因此在iOS中并不能像Android一样随意使用蓝牙进行文件传输(除非你已经越狱)。在iOS中进行蓝牙传输应用开发常用的框架有如下几种:
GameKit.framework:iOS7之前的蓝牙通讯框架,从iOS7开始过期,但是目前多数应用还是基于此框架。
MultipeerConnectivity.framework:iOS7开始引入的新的蓝牙通讯开发框架,用于取代GameKit。
CoreBluetooth.framework:功能强大的蓝牙开发框架,要求设备必须支持蓝牙4.0。
前两个框架使用起来比较简单,但是缺点也比较明显:仅仅支持iOS设备,传输内容仅限于沙盒或者照片库中用户选择的文件,并且第一个框架只能在同一个应用之间进行传输(一个iOS设备安装应用A,另一个iOS设备上安装应用B是无法传输的)。当然CoreBluetooth就摆脱了这些束缚,它不再局限于iOS设备之间进行传输,你可以通过iOS设备向Android、Windows Phone以及其他安装有蓝牙4.0芯片的智能设备传输,因此也是目前智能家居、无线支付等热门智能设备所推崇的技术。
本文要介绍的CoreBluetooth从iphone4s开始支持,专门用于与BLE设备通讯。
二 设备和特征
什么是服务和特征呢(service and characteristic)?
每个蓝牙4.0的设备都是通过服务和特征来展示自己的,一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征。特征是与外界交互的最小单位。比如说,一台蓝牙4.0设备,用特征A来描述自己的出厂信息,用特征B来与收发数据等。
服务和特征都是用UUID来唯一标识的,UUID的概念如果不清楚请自行google,国际蓝牙组织为一些很典型的设备(比如测量心跳和血压的设备)规定了标准的service UUID。例如,我所用的设备UUIID如下
#define BLE_SERVICE_UUID @"9F989D1F-9E7B-B4D7-9322-59F2DCC2D646"
三 实现过程
1 建立中心角色
首先在我自己类的头文件中要包含CoreBluetooth的头文件,并继承两个协议<CBCentralManagerDelegate,CBPeripheralDelegate>,代码如下:
2 扫描外设(discover)
//步骤2 扫描 [self.manager scanForPeripheralsWithServices:nil options:nil];
3连接外设(connect)
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ NSLog(@"找到一台设备:%@ UUID:%@ RSSI:%@ advertisementData:%@ 名称:%@ 信号强度:%d",peripheral,peripheral.UUID ,RSSI,advertisementData,peripheral.name,[RSSI intValue]); <span style="white-space:pre"> </span> if ([peripheral.name isEqualToString:DEVICE_NAME]){ //连接设备 [self connect:peripheral]; } }
//连接指定的设备 -(BOOL)connect:(CBPeripheral *)peripheral { NSLog(@"connect start"); _testPeripheral = nil; [manager connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]]; //开一个定时器监控连接超时的情况 connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(connectTimeout:) userInfo:peripheral repeats:NO]; return (YES); }
4扫描外设中的服务和特征(discover)
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { [connectTimer invalidate]; NSLog(@"Did connect to peripheral: %@", peripheral); _testPeripheral = peripheral; [peripheral setDelegate:self]; [peripheral discoverServices:nil]; }
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"didDiscoverServices"); if (error) { NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]); //失败处理 return; } for (CBService *service in peripheral.services) { if ([service.UUID isEqual:[CBUUID UUIDWithString:<span style="color: rgb(120, 73, 42); font-family: Menlo; font-size: 11px; line-height: 26px;">BLE_SERVICE_UUID</span>]]) { NSLog(@"Service found with UUID: %@", service.UUID); [peripheral discoverCharacteristics:nil forService:service]; break; } } }
5与外设做数据交互(explore and interact)
<ol start="1" class="dp-objc" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 26px; margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">-(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)msrRead </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">{ </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> unsigned <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">char</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> command[</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] = {</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">}; </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> unsigned <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">char</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">char</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *pTmp; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> nSendLen = </span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> unsigned <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">char</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ucCrc[</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">3</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">] = {</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">}; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> _commandType = COMMAND_MSR_READ; </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp = command; </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">x</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//start</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">xc</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//main cmd</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">x</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">7</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//sub cmd</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> nSendLen = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = nSendLen/<span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = nSendLen%<span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">5</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">x</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//sub cmd</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> *pTmp = <span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">x</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span><span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); background-color: inherit;">//sub cmd</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> pTmp++; </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Crc<span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">6</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">CCITT(command+</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,pTmp-command-</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">1</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,ucCrc); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> memcpy(pTmp,ucCrc,<span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">2</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">NSData</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> *data = [[NSData</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> alloc</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> initWithBytes</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:&command</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> length</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">9</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> NSLog(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">@"send data:%@"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, data); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [g_BLEInstance<span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.recvData</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> setLength</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:</span><span class="xcodenumber" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">0</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">]; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> [g_BLEInstance<span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;"> writeChar</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">:data]; </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} </span></li></ol>
<pre name="code" class="objc">//收到数据 - (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]; }
// 解码返回的数据 - (void)decodeData:(NSData *)receiveData{ }
6断开连接(disconnect)。
if (self.peripheral != nil) { self.connected = NO; [self.manager cancelPeripheralConnection:self.peripheral]; }