我是靠谱客的博主 缓慢书本,最近开发中收集的这篇文章主要介绍蓝牙串口透传-从机发送与接收(整理),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、从机通过串口发送数据给主机(Notification:character4)

//simpleBLEPeripheral.c
//串口回调函数
static void NpiSerialCallback(uint8 port,uint8 events)
{
    (void)port;
    uint8 buf[128];
    int i ;
    for(i=6000;i>0;i--)asm("nop");
    if( events & HAL_UART_RX_TIMEOUT )
    {
        tx_numBytes = NPI_RxBufLen();
        if(tx_numBytes)
        {
            NPI_ReadTransport(buf,tx_numBytes);
            NPI_WriteTransport(buf,tx_numBytes);
            SerialSendNoti(buf,tx_numBytes); 
            tx_numBytes = 0;
        }
    }
}
static void SerialSendNoti(uint8 *pBuffer,uint16 length)
{
    uint8 len;
    if(length > 20)
        len = 20;
    else
        len = length;
    static attHandleValueNoti_t pReport;
    pReport.handle=0x30;      //CHA4 =simpleProfileAttrTbl[13]=0x30;
    pReport.len = len;
    osal_memcpy(pReport.value, pBuffer, len);
    GATT_Notification( 0, &pReport, FALSE );
}

//simpleGATTprofile.c

bStatus_t SimpleProfile_SetParameter( uint8 param, uint8 len, void *value )
{
  bStatus_t ret = SUCCESS;
  switch ( param )
  {
    //...
    //...
    //... 
      case SIMPLEPROFILE_CHAR4:
      if ( len == tx_numBytes ) 
      {
        VOID osal_memcpy( simpleProfileChar4, value, tx_numBytes );

        GATTServApp_ProcessCharCfg( simpleProfileChar4Config, simpleProfileChar4, FALSE,
                                    simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
                                    INVALID_TASK_ID );      
      } 
//    else
//    {
//       ret = bleInvalidRange;
//    }
      break;
    //...
    //...
    //...    
    default:
      ret = INVALIDPARAMETER;
      break;
  }

  return ( ret );
}

2、从机接收主机的数据,然后用串口转发

//simpleGATTprofile.c
/*********************************************************************
 * @fn      simpleProfileChangeCB
 *
 * @brief   Callback from SimpleBLEProfile indicating a value change
 *
 * @param   paramID - parameter ID of the value that was changed.
 *
 * @return  none
 */
static void simpleProfileChangeCB( uint8 paramID )
{
    uint8 buffer[20];
    if( paramID==SIMPLEPROFILE_CHAR6 )
    {
        SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, &buffer );
        NPI_WriteTransport(buffer,rx_numBytes);//  将接收到数据的用串口转发              
    }
}
static bStatus_t simpleProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
                                 uint8 *pValue, uint8 len, uint16 offset )
{
  bStatus_t status = SUCCESS;
  uint8 notifyApp = 0xFF; 
  rx_numBytes = len;  //接收数据长度 
  // If attribute permissions require authorization to write, return error
  if ( gattPermitAuthorWrite( pAttr->permissions ) )
  {
    // Insufficient authorization
    return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  }

  if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  {
    // 16-bit UUID
    uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
    switch ( uuid )
    {
        //...
        //...
        //...  
      case SIMPLEPROFILE_CHAR6_UUID:
        if( offset == 0)
        {
//          if( len !=SIMPLEPROFILE_CHAR6_LEN )
            if( rx_numBytes > SIMPLEPROFILE_CHAR6_LEN )
            {
                status = ATT_ERR_INVALID_VALUE_SIZE;
            }
        }
        else
        {
            status = ATT_ERR_ATTR_NOT_LONG;
        }
        if( status == SUCCESS)
        {
//          VOID osal_memcpy(pAttr->pValue,pValue,SIMPLEPROFILE_CHAR6_LEN);
            VOID osal_memcpy(pAttr->pValue,pValue,rx_numBytes);
            notifyApp = SIMPLEPROFILE_CHAR6;
        }            
        break;   
        //...
        //...
        //...       
}

最后

以上就是缓慢书本为你收集整理的蓝牙串口透传-从机发送与接收(整理)的全部内容,希望文章能够帮你解决蓝牙串口透传-从机发送与接收(整理)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部