关于低功耗蓝牙的连接参数更新

Posted xiaoshideyuxiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于低功耗蓝牙的连接参数更新相关的知识,希望对你有一定的参考价值。

首先要明确:连接参数是由主机端确定的,从机端可以发起更新连接参数的请求。主机端可以直接下发连接参数,而从机端则只能是发起更新请求并附带想要的连接参数范围。

在初始化中将想要的连接参数设置进osal( 在和ios设备连接时需要注意一些规则,在前面有提到 ):

GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
    GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
    GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
    GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
    GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );

追踪下GAPRole_SetParameter函数中这些参数的运作:

case GAPROLE_MIN_CONN_INTERVAL:
      
        uint16 newInterval = *((uint16*)pValue);
        if (   len == sizeof ( uint16 )           &&
             ( newInterval >= MIN_CONN_INTERVAL ) &&
             ( newInterval <= MAX_CONN_INTERVAL ) )
        
          gapRole_MinConnInterval = newInterval;
        
        else
        
          ret = bleInvalidRange;
        
      
      break;

都是存储在全局变量中,那么这些参数是何时发出去的呢?
且看:
在建立连接后会产生一个MSG然后在gapRole_ProcessGAPMsg函数中的GAP_LINK_PARAM_UPDATE_EVENT事件
gap.h中有对这一事件的介绍

case GAP_LINK_ESTABLISHED_EVENT:
      
        gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;

        if ( pPkt->hdr.status == SUCCESS )
        
          VOID osal_memcpy( gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN );
          gapRole_ConnectionHandle = pPkt->connectionHandle;
          gapRole_state = GAPROLE_CONNECTED;

          if ( gapRole_RSSIReadRate )
          
            // Start the RSSI Reads
            VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
          

          // Store connection information
          gapRole_ConnInterval = pPkt->connInterval;
          gapRole_ConnSlaveLatency = pPkt->connLatency;
          gapRole_ConnTimeout = pPkt->connTimeout;

          // Check whether update parameter request is enabled
          if ( gapRole_ParamUpdateEnable == TRUE )
          
            // Get the minimum time upon connection establishment before the 
            // peripheral can start a connection update procedure.
            uint16 timeout = GAP_GetParamValue( TGAP_CONN_PAUSE_PERIPHERAL );

            osal_start_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT, timeout*1000 );
          

          // Notify the Bond Manager to the connection
          VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr, pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL );

          // Set enabler to FALSE; device will become discoverable again when
          // this value gets set to TRUE
          gapRole_AdvEnabled = FALSE;          
        
        else if ( pPkt->hdr.status == bleGAPConnNotAcceptable )
        
          // Set enabler to FALSE; device will become discoverable again when
          // this value gets set to TRUE
          gapRole_AdvEnabled = FALSE;

          // Go to WAITING state, and then start advertising
          gapRole_state = GAPROLE_WAITING;
        
        else
        
          gapRole_state = GAPROLE_ERROR;
        
        notify = TRUE;
      
      break;

osal_start_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT, timeout*1000 );
这个事件便是发起连接参数更新,这个timeout是为了让连接更稳定,在建立连接之后得经过一段稳定的时间在更新参数

有网友的做法是在peripheralStateNotificationCB函数中的GAPROLE_CONNECTED里面添加更新参数的函数手动发起更新请求,不知这种做法是否有风险。

GAPRole_SendUpdateParam( uint16 minConnInterval, uint16 maxConnInterval,
                                   uint16 latency, uint16 connTimeout, uint8 handleFailure );

也可以用下面这个中方式,两者本质都是用gapRole_startConnUpdate( GAPROLE_NO_ACTION );


GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_REQ, sizeof ( uint8 ), TRUE );

怎么获取实时连接参数?
好办!
如下函数,注册一个回调函数,然后就可以得到connInterval,connSlaveLatency,connTimeout这三项参数,当参数更新成功是进入回调;因此可以在回调函数里面用LCD,打印,发给APP等等各种方式来展示

GAPRole_RegisterAppCBs( gapRolesParamUpdateCB_t *pParamUpdateCB );

顺便贴一下回调函数的调用:

 case GAP_LINK_PARAM_UPDATE_EVENT:
      
        gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;

        // Cancel connection param update timeout timer (if active)
        VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );

        if ( pPkt->hdr.status == SUCCESS )
        
          // Store new connection parameters
          gapRole_ConnInterval = pPkt->connInterval;
          gapRole_ConnSlaveLatency = pPkt->connLatency;
          gapRole_ConnTimeout = pPkt->connTimeout;

          // Make sure there's no pending connection update procedure
          if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
          
            // Notify the application with the new connection parameters
            if ( pGapRoles_ParamUpdateCB != NULL )
            
              (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval, 
                                          gapRole_ConnSlaveLatency, 
                                          gapRole_ConnTimeout );
            
          
        
      
      break;

就写到这了,其他验证过会再更新

第一次写,有点乱。。。

以上是关于关于低功耗蓝牙的连接参数更新的主要内容,如果未能解决你的问题,请参考以下文章

Android 低功耗Ble 蓝牙4.0多连接 开源框架

Android蓝牙开发——BLE(低功耗蓝牙)(附完整Demo)

Android ble (蓝牙低功耗) 中的坑和技巧

Android 低功耗蓝牙(Ble) 开发总结

低功耗蓝牙通知间隔

缓存低功耗蓝牙?如何解决它