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





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

iOS蓝牙开发(二):iOS 蓝牙4.0中心模式 代码实现

上一篇简单介绍了蓝牙的部分基础知识,详细的东西大家可以去github上搜babyBluetooth,里面有一些学习资料iOS连接外设的流程 建立中心管理者 扫描外设 discoverPeriphera...
  • u010731949
  • u010731949
  • 2017年07月10日 18:19
  • 665

IOS蓝牙4.0使用心得

蓝牙4.0自动连接 数据接收,通过手机/pad与蓝牙4.0的设备进行连接,之后设备上按对应的按键我们会收到对应的数值 蓝牙断开自动连接 蓝牙后台运行...
  • xgcyangguang
  • xgcyangguang
  • 2016年03月30日 14:39
  • 5747

iOS开发—蓝牙4.0(BLE)与外设连接及收发数据的流程

苹果在iOS 6系统之后开始支持BLE 4.0,iPhone4s,iPod 5,iPad 3等之后的机型开始内嵌BLE4.0硬件,因此在开发前请先确认你的开发环境符合上述要求,并且苹果在BLE4.0之...
  • Alexander_Wei
  • Alexander_Wei
  • 2017年04月08日 06:13
  • 3130

iOS开发之蓝牙4.0技术完美实现

前言前端时间,同学在做项目过程中遇到关于蓝牙方面的问题,今天我就给大家进行详细的进行讲解下蓝牙在iOS开发中的具体实现.在介绍蓝牙前,大家要搞清楚什么是蓝牙?什么是蓝牙?随着蓝牙低功耗技术BLE(Bl...
  • BaiHuaXiu123
  • BaiHuaXiu123
  • 2016年05月01日 21:13
  • 14368

iOS开发之蓝牙4.0 BLE开发

物联网时代的到来,智能硬件层出不穷。蓝牙低功耗技术(BLE,Bluetooth Low Energy)使得蓝牙4.0的应用越来越广泛,比如小米手环就是使用蓝牙4.0来传输数据。在蓝牙4.0之前,iOS...
  • taonull
  • taonull
  • 2015年07月22日 17:47
  • 2613

iOS蓝牙4.0开发(一)与外设通讯

蓝牙协议本身经历了从1.0到4.0的升级演变, 最新的4.0以其低功耗著称,所以一般也叫BLE(Bluetoothlow energy)。 iOS 有两个框架支持蓝牙与外设连接。一个是 Extern...
  • junjun150013652
  • junjun150013652
  • 2015年05月11日 17:20
  • 1499

iOS开发 蓝牙4.0的各种亲身经历踩过得坑

1.首先建立这个三个参数@property (nonatomic,strong)CBCentralManager * manager; @property (nonatomic,strong)CBPe...
  • xinyangNSLog
  • xinyangNSLog
  • 2016年03月16日 09:55
  • 1561

iOS开发之蓝牙4.0 -- BLE开发

前言—— 常用概念用语: MFI ( make for ipad ,iphone, itouch)  -------- 专们为苹果设备制作的设备 BLE (buletouch low ...
  • xinzhaoyu
  • xinzhaoyu
  • 2017年05月03日 11:03
  • 319

Swift学习第十三枪-使用Swift开发IOS中蓝牙4.0的开发流程

前面总结了几篇关于Swift的使用,今天要讲的是关于使用Swift开发IOS中蓝牙4.0的开发流程,我以前只是会搞android的蓝牙开发,最近开始了Swift的学习,作为一个swift爱好者,想把蓝...
  • u010046908
  • u010046908
  • 2016年07月04日 13:48
  • 7241

iOS:蓝牙BLE 4.0 开发

iOS蓝牙开发蓝牙常见名称和缩写MFI :make for ipad ,iphone, itouch 专们为苹果设备制作的设备; BLE: buletouch low energy,蓝牙4.0设备因...
  • liuq0725
  • liuq0725
  • 2016年08月17日 16:19
  • 776
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS蓝牙4.0入门
举报原因:
原因补充:

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