zoukankan      html  css  js  c++  java
  • BLE4.0教程三 自定义服务(CC2541)

    1.什么是服务

      蓝牙通信中,我们就是需要对各个服务中的特征值进行交流,现在尝试自定义一个服务。

    2.创建TEMProfile.c、TEMProfile.h

      这是服务的主体文件。

      其里面大致无非是定义特征值,创建读写值的函数,注册函数,增添服务函数,以及包装一些回调函数。

      具体如下。

      (1)所有定义的变量其实都是和特征值相关的,具体详见上篇文章:http://www.cnblogs.com/asam/p/6535374.html

      (2)创建函数

         需要创建的函数如下:

    全局函数:

          

    extern  bStatus_t TEMProfile_GetParameter(  uint8 param,  void  *value);
    extern  bStatus_t TEMProfile_SetParameter(  uint8 param, uint8 len,  void *value);
    extern  bStatus_t TEMProfile_RegisterAppCBs(  TEMProfileCBs_t *appCallback);
    extern  bStatus_t TEMProfile_AddService(  uint32  services  );

    本地函数:

     

    static  uint8 TEMProfile_ReadAttrCB(uint16 connHandle, gattAttribute_t *pAttr,uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen  );
    
    static  bStatus_t TEMProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
                                        uint8 *pValue, uint8 len, uint16 offset );
    static  void  TEMProfile_HandleConnStatusCB(  uint16  connHandle, uint8 changeType  );

    ==小注解===============================================================================

      其中读写的四个函数在此前已经见过。

      这两个回调函数 TEMProfile_ReadAttrCB(),TEMProfile_WriteAttrCB(),是封装在一起的。

      

      

      跟踪下去,发现其是在ADDService函数中被调用,这个函数是一开始任务初始化时,调用的。

      

       故猜想,大概是这个回调函数的指针在初始化增添服务的时候,已经被记录,当通信时其他设备请求读写这个服务时才调用到两个函数。

    ======================================================================================  

      除了四个认识的,还有两个服务初始化用的函数

      bStatus_t TEMProfile_RegisterAppCBs(  TEMProfileCBs_t *appCallback);
      bStatus_t TEMProfile_AddService(  uint32  services  );
     这两个在任务初始化时,被调用。
     前者调用的是在应用层写的。具体后面再讲。

    3.应用层的相关代码    

         (1)添加头文件 #include "XXXprofile.h"

         (2)服务初始化

          在应用层simpleBLEPeripheral.c中的SimpleBLEPeripheral_Init()函数中,增添

     1   //Templete_Service
     2   TEMProfile_ADDService(GATT_ALL_SERVICES);
     3   // Set the TEMProfile Characteristic Values
     4   
     5   uint8 TEMProfile_Char1Vaule=1;
     6   uint8 TEMProfile_Char2Value[TEMPROFILE_CHAR2_LEN]="2017.03.11";
     7   TEMProfile_SetParameter(  TEMPROFILE_CHAR1, sizeof(uint8),  &TEMProfile_Char1Vaule );
     8   TEMProfile_SetParameter(  TEMPROFILE_CHAR2, TEMPROFILE_CHAR2_LEN,  TEMProfile_Char2Value );  
     9   
    10   //register
    11   VOID TEMProfile_RegisterAppCBs(&simpleBLEPeripheral_TEMProfileCBs);

          这时就用到了上面说的两个函数。

          TEMProfile_RegisterAppCBs是注册回调函数。simpleBLEPeripheral_TEMProfileCBs()在WriteAtrr中被调用。被调用的那个函数的含义是当特征值变化时则做出相应动作。但具体依然不理解。

        (3)定义回调函数

     1 static void simpleProfileChangeCB( uint8 paramID )
     2 {
     3   uint8 newValue;
     4 
     5   switch( paramID )
     6   {
     7     case SIMPLEPROFILE_CHAR1:
     8       SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
     9 
    10       #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    11         HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
    12       #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    13 
    14       break;
    15 
    16     case SIMPLEPROFILE_CHAR3:
    17       SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
    18 
    19       #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    20         HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
    21       #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    22 
    23       break;
    24 
    25     default:
    26       // should not reach here!
    27       break;
    28   }
    29 }

     

    (4)包装一下回调函数

    1 static TEMProfileCBs_t simpleBLEPeripheral_TEMProfileCBs =
    2 {
    3 TEMProfileChangeCB
    4 };

     

    至此,完成一个服务的建立

  • 相关阅读:
    Django学习案例一(blog):四. 使用Admin
    Django学习案例一(blog):三. 模型生成数据
    Django学习案例一(blog):二. 连接数据库
    连接Xively云
    undefined reference to `_sbrk', `_write', `_lseek', `_read'
    Android manifest
    关于android socket出现at java.net.DatagramSocket java.net.BindException at libcore.io.IoBridge.bind(IoBridge.java:89)等waring
    virtual box Failed to load unit ""pgm" 的error
    Lwip lwip_recvfrom函数一个数据包不能分多次读取。
    获取Window下的文件缩略图
  • 原文地址:https://www.cnblogs.com/asam/p/6535641.html
Copyright © 2011-2022 走看看