iOS学习

iOS之蓝牙4.0 BLE相关

2015-12-11  本文已影响780人  陈长见

由于最近工作的东家是一家物联网公司,网上BLE相关的资料确实比较少,尤其我还做了一些调试和加密相关的工作.
由于调试和获取数据的一些问题,自己一开始做开发的时候也不是那么从容.所以决定总结一下我在工作中遇到的问题和情况,希望这些东西能对其他做BLE相关的亲们有点帮助,其实也是对自己的情况的不断总结

一: 文件的初始化和配置

  1. 创建一个控制器,我这里就取名称为CJCenterManagerVC,并包含头文件

    #import <CoreBluetooth/CoreBluetooth.h>
    
  2. 遵守协议:

    @interface CJCenterManagerVC() <CBCentralManagerDelegate, CBPeripheralDelegate>
    

3.如果在开发中用到数据库的话还要配置数据库,libsqlite3.tdb框架,具体位置如下:


二: 蓝牙BLE4.0具体思路及其讲解

声明属性:

   @interface CJCenterManagerVC () <CBCentralManagerDelegate, CBPeripheralDelegate>

   /** 中心管理者 */
   @property (nonatomic, strong) CBCentralManager *cMgr;
   /** 连接到的外设 */
   @property (nonatomic, strong) CBPeripheral *peripheral;
   @property (nonatomic, assign) sqlite3 *db;

   @end

初始化:

- (void)viewDidLoad 
{
  [self cMgr];
}

// 只要中心管理者初始化,就会触发此代理方法, 在这里判断中心管理者的状态是否开启,如果开启,就搜索外设
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state) {
    case CBCentralManagerStateUnknown:
        NSLog(@"CBCentralManagerStateUnknown");
        break;
    case CBCentralManagerStateResetting:
        NSLog(@"CBCentralManagerStateResetting");
        break;
    case CBCentralManagerStateUnsupported:
        NSLog(@"CBCentralManagerStateUnsupported");
        break;
    case CBCentralManagerStateUnauthorized:
        NSLog(@"CBCentralManagerStateUnauthorized");
        break;
    case CBCentralManagerStatePoweredOff:
        NSLog(@"CBCentralManagerStatePoweredOff");
        break;
    case CBCentralManagerStatePoweredOn:
    {
        NSLog(@"CBCentralManagerStatePoweredOn");
        // 在中心管理者成功开启后再进行一些操作
        // 搜索外设
        [self.cMgr scanForPeripheralsWithServices:nil // 通过某些服务筛选外设
                                          options:nil]; // dict,条
        
    }
        break;
        
    default:
        break;
  }
}

 - (void)ccj_dismissConentedWithPeripheral:(CBPeripheral 
 *)peripheral IsCancle:(BOOL)cancle
      {
          // 停止扫描
          [self.cMgr stopScan];
          if (cancle) {
        // 断开连接
        [self.cMgr cancelPeripheralConnection:peripheral];
          }
    
      }


      // 读RSSI值
      - (void)readRSSIInfo
      {
         [self.peripheral readRSSI];
      }

发现外设的特征的描述数组:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error
{
// 在此处读取描述即可
for (CBDescriptor *descriptor in characteristic.descriptors) {
// 它会触发
[peripheral readValueForDescriptor:descriptor];
}

备注: 在安卓端,只要控制了readRSSIInfo的方法调用频率, 下面的回调方法可以按照频率来调用,比如说0.1秒,但是我亲自试过, 在我们iOS端,如果你控制了readRSSIInfo的调用频率,这个频率大于1秒钟,那么就会按照你所控制的频率进行下面的方法回调,但是如果小于1秒钟,都是默认为1秒钟调用下面的回调方法一次! 所以也就是说RSSI的值只能最快1秒钟获取一次

  //读取到信号回调
  -(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error
  {
      
  }
 - (void)ccj_dismissConentedWithPeripheral:(CBPeripheral *)peripheral IsCancle:(BOOL)cancle
 {
          // 停止扫描
       [self.cMgr stopScan];
       if (cancle) 
       {
           // 断开连接
           [self.cMgr cancelPeripheralConnection:peripheral];
        }
    
 }

三: 其他

关于蓝牙的加密通道的具体实现 和测试的具体实现会写在其他的篇幅中,如果发现本博客哪处有问题或者您有任何问题,欢迎留言指正和探讨,让我们共同进步!

上一篇 下一篇

猜你喜欢

热点阅读