iOS蓝牙4.0入门

原创 2015年07月06日 17:47:48

一 蓝牙介绍

随着蓝牙低功耗技术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芯片的智能设备传输,因此也是目前智能家居、无线支付等热门智能设备所推崇的技术。

本文要介绍的CoreBluetoothiphone4s开始支持,专门用于与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>,代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <CoreBluetooth/CoreBluetooth.h>  
  2. CBCentralManager *manager;  
  3. manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]


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;  
        }  
          
          
    }  
}  

  1. - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error   
  2. {  
  3.       
  4.     if (error)   
  5.     {  
  6.         NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);  
  7.           
  8.         if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectChar:withPeripheral:error:)])  
  9.             [self.delegate DidNotifyFailConnectChar:nil withPeripheral:nil error:nil];  
  10.           
  11.         return;  
  12.     }  
  13.       
  14.       
  15.     for (CBCharacteristic *characteristic in service.characteristics)  
  16.     {  
  17.         if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_TX]])  
  18.         {  
  19.             NSLog(@"Discovered read characteristics:%@ for service: %@", characteristic.UUID, service.UUID);  
  20.               
  21.             _readCharacteristic = characteristic;//保存读的特征  
  22.               
  23.             if ([self.delegate respondsToSelector:@selector(DidFoundReadChar:)])  
  24.                 [self.delegate DidFoundReadChar:characteristic];  
  25.               
  26.             break;  
  27.         }  
  28.     }  
  29.   
  30.       
  31.     for (CBCharacteristic * characteristic in service.characteristics)  
  32.     {  
  33.           
  34.           
  35.         if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_TRANS_RX]])  
  36.         {  
  37.   
  38.             NSLog(@"Discovered write characteristics:%@ for service: %@", characteristic.UUID, service.UUID);  
  39.             _writeCharacteristic = characteristic;//保存写的特征  
  40.               
  41.             if ([self.delegate respondsToSelector:@selector(DidFoundWriteChar:)])  
  42.                 [self.delegate DidFoundWriteChar:characteristic];  
  43.               
  44.             break;  
  45.               
  46.               
  47.         }  
  48.     }  
  49.       
  50.     if ([self.delegate respondsToSelector:@selector(DidFoundCharacteristic:withPeripheral:error:)])  
  51.         [self.delegate DidFoundCharacteristic:nil withPeripheral:nil error:nil];  
  52.       
  53. }  


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];
    }





版权声明:本文为博主原创文章,未经博主允许不得转载。

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Android--【CSRMesh蓝牙开发】CSRMesh1.3开发

最近公司开始开发蓝牙设备,使用的是csr的芯片开发,由于我是刚入职,公司的iOS工程师早入职,所以iOS版本的APP已经开发完成准备上线了,导致Android这边的开发时间比较急促。刚开始接手,因为C...

ios 蓝牙4.0demo

  • 2013年03月23日 22:39
  • 113KB
  • 下载

iOS 蓝牙 4.0

  • 2016年09月01日 21:56
  • 170KB
  • 下载

蓝牙4.0入门(2)开始程序编写(三)BLE的编写

3.BLE的编写   蓝牙程序的编写主要是作为从机s110,和主机s120以及主从机s130。都是需要先下载。教程可参见hex下载教程。 主要参考的资料:n36 (里面详细阐述了第一个ble程序,入门...

ios蓝牙4.0

  • 2017年02月17日 10:57
  • 1.14MB
  • 下载

iOS蓝牙4.0通信

  • 2015年01月16日 15:03
  • 136KB
  • 下载

IOS蓝牙4.0与读卡器通讯

蓝牙协议本身经历了从1.0到4.0的升级演变, 最新的4.0以其低功耗著称,所以一般也叫BLE(Bluetoothlow energy)。 iOS 有两个框架支持蓝牙与外设连接。一个是 Externa...

蓝牙4.0 iOS 开发源码

  • 2015年04月17日 17:39
  • 329KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS蓝牙4.0入门
举报原因:
原因补充:

(最多只允许输入30个字)