1. 程式人生 > >藍芽串列埠透傳-從機發送與接收(整理)

藍芽串列埠透傳-從機發送與接收(整理)

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