首先要指出,字段属性有notify的不能同时有read,write属性,别问哥,哥也不知道,反正我做的就不能notify,只能read,write。

分享的程序段第一字段有notify属性,第二字段read,write属性。

费话少说,看代码,H文件:

#ifndef LOCKER_H
#define LOCKER_H#ifdef __cplusplus
extern "C"
{
#endif#define SERVAPP_NUM_ATTR_SUPPORTED              (1+4+3)// Simple Profile Service UUID
#define LOCKERPROFILE_SERV_UUID                 0xFFF0#define LOCKERPROFILE_NOTIFY_UUID               0xFFF1
#define LOCKERPROFILE_NOTIFY_MAX_LEN            5  #define LOCKERPROFILE_STREAM_UUID               0xFFF2
#define LOCKERPROFILE_STREAM_MAX_LEN            5  #define SIMPLEPROFILE_SERVICE               0x00000001
/********************************************************************** Profile Callbacks*/
// Callback when a characteristic value has changed
typedef void (*app_input_cb_t)( uint16 uuid ,uint8 *pdata, int len);/********************************************************************** lockerProfile_AddService- Initializes the Simple GATT Profile service by registering*          GATT attributes with the GATT server.* @param   services - services to add. This is a bit map and can contain more than one service.* @param   cb - the function will be called, while service receive a message .
* return    always return SUCCESS*/
bStatus_t lockerProfile_AddService( uint32 services ,app_input_cb_t cb);
/** lockerProfile_AddService - Set a Simple GATT Profile parameter.*    uuid - which service's uuid as sender target*    len - length of data to right*    value - pointer to data to write.  This is dependent on*          the parameter uuid and WILL be cast to the appropriate *          data type .*/
bStatus_t locker_send( uint16 uuid,void *pdata , uint8 len);
/** locker_Read - Get a Simple GATT Profile parameter by uuid.*    param - Profile uuid*    value - pointer to data to write.  This is dependent on*          the parameter ID and WILL be cast to the appropriate *          data type .
*   return : return data length, if op' failse it'll return -1;*/
int locker_Read( uint16 uuid, void *pdata );#ifdef __cplusplus
}
#endif#endif /* SIMPLEGATTPROFILE_H */

  C文件:

/********************************************************************** INCLUDES*/
#include "bcomdef.h"
#include "OSAL.h"
#include "linkdb.h"
#include "att.h"
#include "gatt.h"
#include "gatt_uuid.h"
#include "gattservapp.h"
#include "gapbondmgr.h"#include "locker.h"
#include "stdio.h"/********************************************************************** GLOBAL VARIABLES*/
// Simple GATT Profile Service UUID: 0xFFF0
CONST uint8 lockerSvrUUID[ATT_BT_UUID_SIZE] =
{ LO_UINT16(LOCKERPROFILE_SERV_UUID), HI_UINT16(LOCKERPROFILE_SERV_UUID)
};
// notify UUID: 0xFFF1
CONST uint8 lockerNotifyUUID[ATT_BT_UUID_SIZE] =
{ LO_UINT16(LOCKERPROFILE_NOTIFY_UUID), HI_UINT16(LOCKERPROFILE_NOTIFY_UUID)
};
// stream UUID: 0xFFF2
CONST uint8 lockerStreamUUID[ATT_BT_UUID_SIZE] =
{ LO_UINT16(LOCKERPROFILE_STREAM_UUID), HI_UINT16(LOCKERPROFILE_STREAM_UUID)
};
/********************************************************************** LOCAL VARIABLES*/
static app_input_cb_t app_input_cb = NULL;
/********************************************************************** Profile Attributes - variables*/
// locker Profile Service attribute
static CONST gattAttrType_t lockerProfileService = { ATT_BT_UUID_SIZE, lockerSvrUUID };// locker Profile Notify Properties
static uint8 lockerNotifyProps = GATT_PROP_NOTIFY;
static uint8 lockerNotifyStream[LOCKERPROFILE_NOTIFY_MAX_LEN];
static uint8 lockerNotifyDesp[17] = "notify\0";
static gattCharCfg_t lockerNotifyConfig[GATT_MAX_NUM_CONN];    // locker Profile Stream Properties
static uint8 lockerStreamProps = GATT_PROP_READ | GATT_PROP_WRITE;
static uint8 lockerStream[LOCKERPROFILE_STREAM_MAX_LEN];
static uint8 lockerStreamDesp[17] = "stream\0";
/********************************************************************** Profile Attributes - Table*/static gattAttribute_t LockerProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =
{// Simple Profile Service{ { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */GATT_PERMIT_READ,                         /* permissions */0,                                        /* handle */(uint8 *)&lockerProfileService            /* pValue */},/**************notify attribute**********************/// notify  Declaration{ { ATT_BT_UUID_SIZE, characterUUID },GATT_PERMIT_READ, 0,&lockerNotifyProps },// notify stream buffer { { ATT_BT_UUID_SIZE, lockerNotifyUUID },0, 0, lockerNotifyStream },// notify stream  configuration{ { ATT_BT_UUID_SIZE, clientCharCfgUUID },GATT_PERMIT_READ | GATT_PERMIT_WRITE, 0, (uint8 *)lockerNotifyConfig },// notify stream  Description{ { ATT_BT_UUID_SIZE, charUserDescUUID },GATT_PERMIT_READ, 0, lockerNotifyDesp },     /************************stream buffer attribute***********************/// stream  Declaration{ { ATT_BT_UUID_SIZE, characterUUID },GATT_PERMIT_READ, 0,&lockerStreamProps },// stream buffer { { ATT_BT_UUID_SIZE, lockerStreamUUID },GATT_PERMIT_READ | GATT_PERMIT_WRITE, 0, lockerStream },//  stream  Description{ { ATT_BT_UUID_SIZE, charUserDescUUID },GATT_PERMIT_READ, 0, lockerStreamDesp },
};/********************************************************************** LOCAL FUNCTIONS*/
static uint8 lockerReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr, uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
static bStatus_t lockerWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,uint8 *pValue, uint8 len, uint16 offset );static void lockerHandleConnStatusCB( uint16 connHandle, uint8 changeType );/********************************************************************** PROFILE CALLBACKS*/
// Simple Profile Service Callbacks
CONST gattServiceCBs_t lockerProfileCBs =
{lockerReadAttrCB,  // Read callback function pointerlockerWriteAttrCB, // Write callback function pointerNULL                       // Authorization callback function pointer
};bStatus_t lockerProfile_AddService( uint32 services ,app_input_cb_t cb)
{uint8 status = SUCCESS;printf("lockerProfile_AddService:  \n");// Initialize Client Characteristic Configuration attributesGATTServApp_InitCharCfg( INVALID_CONNHANDLE, lockerNotifyConfig );// Register with Link DB to receive link status change callbackVOID linkDB_Register( lockerHandleConnStatusCB );  if ( services & SIMPLEPROFILE_SERVICE ){// Register GATT attribute list and CBs with GATT Server Appstatus = GATTServApp_RegisterService( LockerProfileAttrTbl, GATT_NUM_ATTRS( LockerProfileAttrTbl ),&lockerProfileCBs );}app_input_cb = cb;return ( status );
}
bStatus_t locker_send( uint16 uuid,void *pdata , uint8 len)
{bStatus_t ret = SUCCESS;switch ( uuid ){case LOCKERPROFILE_NOTIFY_UUID:if ( len <= LOCKERPROFILE_NOTIFY_MAX_LEN) {printf("locker_send: uuid:%04x notify app \n",uuid);osal_memcpy(lockerNotifyStream,pdata,len);GATTServApp_ProcessCharCfg(lockerNotifyConfig,lockerNotifyStream, FALSE,LockerProfileAttrTbl, GATT_NUM_ATTRS( LockerProfileAttrTbl ),INVALID_TASK_ID );}else{ret = bleInvalidRange;}break;case LOCKERPROFILE_STREAM_UUID:if ( len <= LOCKERPROFILE_STREAM_MAX_LEN) {printf("locker_Send: uuid:%04x write to stream buffer and waite to read \n",uuid);osal_memcpy(lockerStream,pdata,len);}else{ret = bleInvalidRange;}    default:ret = INVALIDPARAMETER;break;}return ( ret );
}int locker_Read( uint16 uuid, void *pdata )
{int ret;printf("locker_Read: uuid:%04x \n",uuid);switch ( uuid ){case LOCKERPROFILE_NOTIFY_UUID:osal_memcpy(pdata,lockerNotifyStream,LOCKERPROFILE_NOTIFY_MAX_LEN);ret = LOCKERPROFILE_NOTIFY_MAX_LEN;break;case LOCKERPROFILE_STREAM_UUID:osal_memcpy(pdata,lockerStream,LOCKERPROFILE_STREAM_MAX_LEN);ret = LOCKERPROFILE_NOTIFY_MAX_LEN;break;default:ret = -1;break;}return ( ret );
}static uint8 lockerReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr, uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
{bStatus_t status = SUCCESS;// If attribute permissions require authorization to read, return errorif ( gattPermitAuthorRead( pAttr->permissions ) ){// Insufficient authorizationreturn ( ATT_ERR_INSUFFICIENT_AUTHOR );}// Make sure it's not a blob operation (no attributes in the profile are long)if ( offset > 0 ){return ( ATT_ERR_ATTR_NOT_LONG );}if ( pAttr->type.len == ATT_BT_UUID_SIZE ){// 16-bit UUIDuint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);printf("lockerProfile_ReadAttrCB: uuid:%04x \n",uuid);switch ( uuid ){// No need for "GATT_SERVICE_UUID" or "GATT_CLIENT_CHAR_CFG_UUID" cases;// gattserverapp handles those reads// characteristics 1 and 2 have read permissions//   can be sent as a notification, it is included herecase LOCKERPROFILE_NOTIFY_UUID:*pLen = LOCKERPROFILE_NOTIFY_MAX_LEN;osal_memcpy(pValue,pAttr->pValue,LOCKERPROFILE_NOTIFY_MAX_LEN);break;    case LOCKERPROFILE_STREAM_UUID:*pLen = LOCKERPROFILE_STREAM_MAX_LEN;osal_memcpy(pValue,pAttr->pValue,LOCKERPROFILE_STREAM_MAX_LEN);break;default:// Should never get here! (characteristics 3 and 4 do not have read permissions)*pLen = 0;status = ATT_ERR_ATTR_NOT_FOUND;break;}}else{// 128-bit UUID*pLen = 0;status = ATT_ERR_INVALID_HANDLE;}return ( status );
}static bStatus_t lockerWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,uint8 *pValue, uint8 len, uint16 offset )
{bStatus_t status = SUCCESS;// If attribute permissions require authorization to write, return errorif ( gattPermitAuthorWrite( pAttr->permissions ) ){// Insufficient authorizationreturn ( ATT_ERR_INSUFFICIENT_AUTHOR );}if ( pAttr->type.len == ATT_BT_UUID_SIZE ){// 16-bit UUIDuint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);printf("lockerProfile_WriteAttrCB: uuid:%04x \n",uuid);switch ( uuid ){case LOCKERPROFILE_NOTIFY_UUID:status = ATT_ERR_INVALID_HANDLE;//Write the value           break;case LOCKERPROFILE_STREAM_UUID:if((offset==0)&(app_input_cb!=NULL)){if(len<=LOCKERPROFILE_STREAM_MAX_LEN)osal_memcpy(lockerStream,pValue,len);app_input_cb(LOCKERPROFILE_STREAM_UUID,pValue,len);}break;case GATT_CLIENT_CHAR_CFG_UUID:status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,offset, GATT_CLIENT_CFG_NOTIFY );break;       default:// Should never get here! (characteristics 2 and 4 do not have write permissions)status = ATT_ERR_ATTR_NOT_FOUND;break;}}else{// 128-bit UUIDstatus = ATT_ERR_INVALID_HANDLE;}return ( status );
}
static void lockerHandleConnStatusCB( uint16 connHandle, uint8 changeType )
{ // Make sure this is not loopback connectionif ( connHandle != LOOPBACK_CONNHANDLE ){// Reset Client Char Config if connection has droppedif ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED )      ||( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) && ( !linkDB_Up( connHandle ) ) ) ){ printf("lockerProfile_HandleConnStatusCB: Reset Client Char Config if connection has dropped \n");GATTServApp_InitCharCfg( connHandle, lockerNotifyConfig );}}
}/*********************************************************************
*********************************************************************/

  在simpleBLEPeripheral.c下增加input函数用于接收数据:

void input(uint16 uuid, uint8 *pdata , int  len)
{int i;printf("input from uuid: %04x len:%d data:",uuid,len);for(i=0;i<len;i++)printf("%02x ",pdata[i]);printf("\n");
}

  初始化时:

void SimpleBLEPeripheral_Init( uint8 task_id )
{simpleBLEPeripheral_TaskID = task_id;// Setup the GAPVOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL );// Setup the GAP Peripheral Role Profile{uint8 initial_advertising_enable = TRUE;// By setting this to zero, the device will go into the waiting state after// being discoverable for 30.72 second, and will not being advertising again// until the enabler is set back to TRUEuint16 gapRole_AdvertOffTime = 0;uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;// Set the GAP Role ParametersGAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );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 );}// Set the GAP CharacteristicsGGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );// Set advertising interval{uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );}// Setup the GAP Bond Manager{uint32 passkey = 0; // passkey "000000"uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;uint8 mitm = TRUE;uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;uint8 bonding = TRUE;GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey );GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode );GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm );GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap );GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding );}// Initialize GATT attributesGGS_AddService( GATT_ALL_SERVICES );            // GAPGATTServApp_AddService( GATT_ALL_SERVICES );    // GATT attributesDevInfo_AddService();                           // Device Information ServicelockerProfile_AddService( GATT_ALL_SERVICES,input);  // Simple GATT Profile// Register for all key events - This app will handle all key eventsRegisterForKeys( simpleBLEPeripheral_TaskID );// Enable clock divide on halt// This reduces active current while radio is active and CC254x MCU// is haltedHCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT );// Setup a delayed profile startuposal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT );}

  good luck!!我的已经工作的,你的呢?

转载于:https://www.cnblogs.com/lort/p/5753218.html

CC2540自己的配置文件相关推荐

  1. CC2540\CC2541 资料整理

    整体 1.TI的蓝牙平台支持2种协议栈/应用配置:单一设备配置.网络处理器配置 2.协议栈最顶层2个通用profile: GAP 通用访问配置文件层 Generic Access Profile GA ...

  2. BLE:CC2540学习笔记

    文章转载参考:http://blog.sina.com.cn/s/articlelist_5617273496_0_1.html BLE:CC2540与CC2541相关寄存器一览 一.关于普通IO操作 ...

  3. CC2540蓝牙协议栈开发简介

    本系列教程将结合TI推出的CC254x SoC 系列,讲解从环境的搭建到蓝牙4.0协议栈的开发来深入学习蓝牙4.0的开发过程.教程共分为六部分,本文为第三部分: 第三部分知识点: 第十一节 串口通信 ...

  4. golang通过RSA算法生成token,go从配置文件中注入密钥文件,go从文件中读取密钥文件,go RSA算法下token生成与解析;go java token共用

    RSA算法 token生成与解析 本文演示两种方式,一种是把密钥文件放在配置文件中,一种是把密钥文件本身放入项目或者容器中. 下面两种的区别在于私钥公钥的初始化, init方法,需要哪种取哪种. 通过 ...

  5. Dockerfile springboot项目拿走即用,将yml配置文件从外部挂入容器

    Dockerfile 将springboot项目jar包打成镜像,并将yml配置文件外挂. # 以一个镜像为基础,在其上进行定制.就像我们之前运行了一个 nginx 镜像的容器,再进行修改一样,基础镜 ...

  6. 在kotlin companion object中读取spring boot配置文件,静态类使用@Value注解配置

    在kotlin companion object中读取配置文件 静态类使用@Value注解配置 class Config {@Value("\${name}")fun setNam ...

  7. 基于Golang的监听读取配置文件的程序包开发——simpleConfig_v1

    基于Golang的监听&读取配置文件的程序包开发--simpleConfig_v1 [阅读时间:约10分钟] 一.配置文件概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 ...

  8. Go 学习笔记(82)— Go 第三方库之 viper(解析配置文件、热更新配置文件)

    1. viper 特点 viper 是一个完整的 Go应用程序的配置解决方案,它被设计为在应用程序中工作,并能处理所有类型的配置需求和格式.支持特性功能如下: 设置默认值 读取 JSON.TOML.Y ...

  9. jupyter qtconsole 配置文件的生成和修改

    jupyter qtconsole 配置文件的生成和修改 安装 Anaconda 之后默认会安装 IPython, 但是输入 ipython qtconsole 之后默认的 Jupyter QtCon ...

最新文章

  1. 【青少年编程】【四级】绘制花瓣
  2. Office 365 成微软 AI 落地载体, 53 项 AI 功能你用过多少?
  3. 初学Oracle的笔记(2)——基础内容(实时更新中..)
  4. MySQL的大分页查询该如何优化?
  5. Python中常用的高阶函数
  6. map 循环_被问到Spring循环依赖怎么解决?秀给面试官看!内附图解
  7. Spring Boot开发Web应用
  8. 使用EndNote X9引用参考文献并在Word中修改生成的引文格式(编号、字体大小)GBT7714(numeric)
  9. LeetCode(860)——柠檬水找零(JavaScript)
  10. Shell脚本学习-阶段十六-备份和恢复系统权限
  11. Linux——vi命令详解
  12. linux下find搜索jpg格式图片,Linux文件查找命令-find
  13. ASCII码值是怎么计算的,怎么计算arccos的值
  14. 《Linux C/C++服务器开发实践》简介
  15. 1253寻找肇事司机
  16. Kaggle:Quora Question Pairs
  17. Redis-3-Java搭建Redis
  18. elastic stack 基础组件beats详解
  19. HPUNIX环境常用查看硬件设备信息命令小结
  20. Redis 常用运维命令

热门文章

  1. 谷粒学院day09——课程发布与阿里云视频点播服务
  2. linux shell脚本中打开另一个终端并在新终端中执行shell脚本
  3. 前端面试之ES5与ES6的区别
  4. hsql统计两天数据差异的算法及lag()/led()分析函数的使用
  5. 文本数据分析的作用是什么?文本数据分析可采用哪些方法?
  6. matlab多元函数数值积分,(数值积分)多元函数的某一变量进行定积分,int积分不出...
  7. 上、下运动神经元的区别
  8. python版植物大战僵尸源码_基于python的植物大战僵尸游戏设计与实现.docx
  9. 引入纯 ESM 模块化的第三方包
  10. 【STM32】标准库与HAL库对照学习教程八--串口通信详解