给 TI BLE 添加自己UUID

来源:互联网 发布:国家行政学院博士 知乎 编辑:程序博客网 时间:2024/06/06 03:13

本文摘录于:http://blog.csdn.net/liqinghan/article/details/40428941


给 TI BLE 添加自己UUID

个人觉得这里的UUID就像USB报告的里面的ID作用一样,一个USB的设备可以根据ID读写不同数据长度的数据块,蓝牙也如此通过特性的UUID来识别读写不同长度的数据块!

可以参考demo的给 的 SIMPLEPROFILE_CHAR1 的样式

我这里使用 CHAR5 因为在demo中CHAR5 没有配置成我想要的,我就配置为 16byte的数据长度的可读写!


simpleGATTprofile.h 文件

1、

[cpp] view plain copy
  1. // Profile Parameters  
  2. #define SIMPLEPROFILE_CHAR1                   0  // RW uint8 - Profile Characteristic 1 value   
  3. #define SIMPLEPROFILE_CHAR2                   1  // RW uint8 - Profile Characteristic 2 value  
  4. #define SIMPLEPROFILE_CHAR3                   2  // RW uint8 - Profile Characteristic 3 value  
  5. #define SIMPLEPROFILE_CHAR4                   3  // RW uint8 - Profile Characteristic 4 value  
  6. #define SIMPLEPROFILE_CHAR5                   4  // RW uint8 - Profile Characteristic 4 valuet  
  7.   
  8. // Key Pressed UUID  
  9. #define SIMPLEPROFILE_CHAR1_UUID            0xFFF1  
  10. #define SIMPLEPROFILE_CHAR2_UUID            0xFFF2  
  11. #define SIMPLEPROFILE_CHAR3_UUID            0xFFF3  
  12. #define SIMPLEPROFILE_CHAR4_UUID            0xFFF4  
  13. #define SIMPLEPROFILE_CHAR5_UUID            0xFFF5   
  14.   
  15. // Length of Characteristic 5 in bytes  
  16. #define SIMPLEPROFILE_CHAR5_LEN           16   //数据长度  

simpleGATTprofile.c文件

[cpp] view plain copy
  1. // Characteristic 5 UUID: 0xFFF5  
  2. CONST uint8 simpleProfilechar5UUID[ATT_BT_UUID_SIZE] =  
  3. {   
  4.   LO_UINT16(SIMPLEPROFILE_CHAR5_UUID), HI_UINT16(SIMPLEPROFILE_CHAR5_UUID)  
  5. };  
  6.   
  7. // Simple Profile Characteristic 5 Properties  
  8. static uint8 simpleProfileChar5Props = GATT_PROP_READ |  GATT_PROP_WRITE ;  
  9.   
  10.   
  11. // Characteristic 16 Value  
  12. static uint8 simpleProfileChar5[SIMPLEPROFILE_CHAR5_LEN] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };  
  13.   
  14.   
  15. // Simple Profile Characteristic 5 User Description  
  16. static uint8 simpleProfileChar5UserDesp[17] = "Characteristic 5\0";  

//如果要添加自己的,注意SERVAPP_NUM_ATTR_SUPPORTED这个下标值

[cpp] view plain copy
  1. static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED]={  
  2. ...  
  3. ...  
  4.  // Characteristic 5 Declaration  
  5.     {   
  6.       { ATT_BT_UUID_SIZE, characterUUID },  
  7.       GATT_PERMIT_READ ,  
  8.       0,  
  9.       &simpleProfileChar5Props   
  10.     },  
  11.   
  12.   
  13.       // Characteristic Value 5  
  14.       {   
  15.         { ATT_BT_UUID_SIZE, simpleProfilechar5UUID },  
  16.         GATT_PERMIT_READ | GATT_PERMIT_WRITE,    
  17.         0,   
  18.         simpleProfileChar5   
  19.       },  
  20.   
  21.   
  22.       // Characteristic 5 User Description  
  23.       {   
  24.         { ATT_BT_UUID_SIZE, charUserDescUUID },  
  25.         GATT_PERMIT_READ,   
  26.         0,   
  27.         simpleProfileChar5UserDesp   
  28.       },  
  29. }  
[cpp] view plain copy
  1. bStatus_t SimpleProfile_SetParameter( uint8 param, uint8 len, void *value ){  
  2. ....  
  3.     case SIMPLEPROFILE_CHAR5:  
  4.       if ( len == SIMPLEPROFILE_CHAR5_LEN )   
  5.       {  
  6.         VOID osal_memcpy( simpleProfileChar5, value, SIMPLEPROFILE_CHAR5_LEN );  
  7.       }  
  8.       else  
  9.       {  
  10.         ret = bleInvalidRange;  
  11.       }  
  12.       break;  
  13. ....  
  14. }  



[cpp] view plain copy
  1. bStatus_t SimpleProfile_GetParameter( uint8 param, void *value ){  
  2. ....  
  3.  case SIMPLEPROFILE_CHAR5:  
  4.       VOID osal_memcpy( value, simpleProfileChar5, SIMPLEPROFILE_CHAR5_LEN );  
  5.       break;        
  6. .....  
  7. }  
[cpp] view plain copy
  1. static uint8 simpleProfile_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,   
  2.                             uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )  
  3. {  
  4. ....  
  5.   case SIMPLEPROFILE_CHAR5_UUID:  
  6.         *pLen = SIMPLEPROFILE_CHAR5_LEN;  
  7.         VOID osal_memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR5_LEN );  
  8.         break;  
  9. ...  
  10. }  

[cpp] view plain copy
  1. static bStatus_t simpleProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,  
  2.                                  uint8 *pValue, uint8 len, uint16 offset ){  
  3. ..  
  4. case SIMPLEPROFILE_CHAR5_UUID:  
  5. //Validate the value  
  6.         // Make sure it's not a blob oper  
  7.         if ( offset == 0 )  
  8.         {  
  9.           if ( len != SIMPLEPROFILE_CHAR5_LEN )  
  10.           {  
  11.             status = ATT_ERR_INVALID_VALUE_SIZE;  
  12.           }  
  13.         }  
  14.         else  
  15.         {  
  16.           status = ATT_ERR_ATTR_NOT_LONG;  
  17.         }  
  18.           
  19.         //Write the value  
  20.         if ( status == SUCCESS )  
  21.         {  
  22.            osal_memcpy(pAttr->pValue,pValue,SIMPLEPROFILE_CHAR5_LEN);  
  23.            notifyApp = SIMPLEPROFILE_CHAR5;          
  24.         }   
  25. break;  
  26. ...  
  27. }  

//app

// simpleBLEPeripheral.c

[cpp] view plain copy
  1. void SimpleBLEPeripheral_Init( uint8 task_id ){  
  2. ..  
  3.  uint8 charValue1 = 1;  
  4.     uint8 charValue2 = 2;  
  5.     uint8 charValue3 = 3;  
  6.     uint8 charValue4 = 4;  
  7.     uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN];  
  8.     osal_memset(charValue5,0,SIMPLEPROFILE_CHAR5_LEN);  
  9.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );  
  10.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );  
  11.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );  
  12.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );  
  13.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );  
  14. ..  
  15. }  
[cpp] view plain copy
  1. static void simpleProfileChangeCB( uint8 paramID ){  
  2. uint8 temp[HAL_LCD_MAX_BUFF];  
  3. //前面的省略.....  
  4.     case SIMPLEPROFILE_CHAR5:  
  5. SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR5, temp ); //获取特征值  
  6. //可以对自己的数据进行分析,实现自己想要的功能,  
  7. //这里仅仅用来串口和LCD打印出来  
  8. temp[SIMPLEPROFILE_CHAR5_LEN] = '\0';  
  9.   
  10. #if (defined HAL_LCD) && (HAL_LCD == TRUE)  
  11. ;  
  12. //HalLcdWriteStringValue( "Char 5:", (uint16)(newValue), 10,HAL_LCD_LINE_3 );  
  13. #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)  
  14. #if(HAL_OLED)  
  15. LCD_CLSLineAt8x16Pix(CLS_START_LINE3, CLS_LINE2);  
  16. LCD_P8x16Str(0,WRITE_START_LINE3,"Char 5:");  
  17. LCD_P8x16Str(0,WRITE_START_LINE4,temp);  
  18. uart_printNotline("Char 5:");  
  19. uart_printLine((char*)temp);  
  20. #endif  
  21.     break;  
  22. <pre code_snippet_id="574739" snippet_file_name="blog_20150108_9_5222020" name="code" class="cpp">//省略.....  
[cpp] view plain copy
  1. }  



实验结果:

使用CC2540 Dongle 作为 central 打开Btool完成连接,



利用UUID通信,读写数据



central发送数据:

串口输出



液晶屏的输出:


原创粉丝点击