一、简介
本文介绍如何在SimpleBLEPeripheral工程中,以char7为例。实现用手机端APP更改蓝牙设备名称.
二、实验平台
编译环境:IAR820.02
协议栈:BLE-CC254x-1.40
代码查看器:Source Insight 3.5
硬件平台:SmartRf 开发板
安卓APP平台: SmartRf
三、编写声明
作者:爱已oО欠费
相关CC2541群:606444519
请完成以下博文:
【cc2541历程】ds18b20发送数值到App:http://blog.csdn.net/wangjiawu7/article/details/72648402
五、硬件原理图暂无
#define SIMPLEPROFILE_CHAR1 0 // RW uint8 - Profile Characteristic 1 value
#define SIMPLEPROFILE_CHAR2 1 // RW uint8 - Profile Characteristic 2 value
#define SIMPLEPROFILE_CHAR3 2 // RW uint8 - Profile Characteristic 3 value
#define SIMPLEPROFILE_CHAR4 3 // RW uint8 - Profile Characteristic 4 value
#define SIMPLEPROFILE_CHAR5 4 // RW uint8 - Profile Characteristic 5 value
#define SIMPLEPROFILE_CHAR6 5 //RW uint8 - Profile Characteristic 6 value
#define SIMPLEPROFILE_CHAR7 6 // RW uint8 - Profile Characteristic 7 value
#define SIMPLEPROFILE_CHARA 9 //RW uint8 - Profile Characteristic A value
// Simple Profile Service UUID
#define SIMPLEPROFILE_SERV_UUID 0xFFF0
// Key Pressed UUID
#define SIMPLEPROFILE_CHAR1_UUID 0xFFF1
#define SIMPLEPROFILE_CHAR2_UUID 0xFFF2
#define SIMPLEPROFILE_CHAR3_UUID 0xFFF3
#define SIMPLEPROFILE_CHAR4_UUID 0xFFF4
#define SIMPLEPROFILE_CHAR5_UUID 0xFFF5
#define SIMPLEPROFILE_CHAR6_UUID 0xFFF6
#define SIMPLEPROFILE_CHAR7_UUID 0xFFF7
#define SIMPLEPROFILE_CHARA_UUID 0xFFFA
// Simple Keys Profile Services bit fields
#define SIMPLEPROFILE_SERVICE 0x00000001
// Length of Characteristic 5 in bytes
#define SIMPLEPROFILE_CHAR5_LEN 5
#define SIMPLEPROFILE_CHAR6_LEN 4 // 温度传输
#define SIMPLEPROFILE_CHAR7_LEN 20 // 设备名称
#define SIMPLEPROFILE_CHARA_LEN 4 // pwm 输出
// Characteristic 7 UUID: 0xFFF7
CONST uint8 simpleProfilechar7UUID[ATT_BT_UUID_SIZE] =
{
LO_UINT16(SIMPLEPROFILE_CHAR7_UUID), HI_UINT16(SIMPLEPROFILE_CHAR7_UUID)
};
// Simple Profile Characteristic 7 Properties
static uint8 simpleProfileChar7Props = GATT_PROP_READ | GATT_PROP_WRITE;
// Characteristic 5 Value
static uint8 simpleProfileChar7[SIMPLEPROFILE_CHAR7_LEN] = {0};
static uint8 simpleProfileChar7Len = 0;
// Simple Profile Characteristic 7 User Description
static uint8 simpleProfileChar7UserDesp[19] = "Device Name Config\0";
#define SERVAPP_NUM_ATTR_SUPPORTED 27//24//20//17
// Characteristic 7 Declaration
{
{ ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&simpleProfileChar7Props
},
// Characteristic Value 7
{
{ ATT_BT_UUID_SIZE, simpleProfilechar7UUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
simpleProfileChar7
},
// Characteristic 7 User Description
{
{ ATT_BT_UUID_SIZE, charUserDescUUID },
GATT_PERMIT_READ,
0,
simpleProfileChar7UserDesp
},
case SIMPLEPROFILE_CHAR7:
case SIMPLEPROFILE_CHARA:
ret = bleInvalidRange;
break;
case SIMPLEPROFILE_CHAR7:
VOID osal_memcpy( value, simpleProfileChar7, simpleProfileChar7Len );
*returnBytes = simpleProfileChar7Len;
break;
case SIMPLEPROFILE_CHAR7_UUID:
case SIMPLEPROFILE_CHAR6_UUID:
case SIMPLEPROFILE_CHARA_UUID:
{
uint8 newValue[20];
extern uint8 simpleProfileReadConfig(uint16 uuid, uint8 *newValue);
*pLen = simpleProfileReadConfig(uuid, newValue);
VOID osal_memcpy( pValue, newValue, *pLen );
}
break;
case SIMPLEPROFILE_CHAR7_UUID:
if ( offset == 0 )
{
if ( len > SIMPLEPROFILE_CHAR7_LEN )
{
status = ATT_ERR_INVALID_VALUE_SIZE;
}
}
else
{
status = ATT_ERR_ATTR_NOT_LONG;
}
//Write the value
if ( status == SUCCESS )
{
VOID osal_memcpy( pAttr->pValue, pValue, len );
simpleProfileChar7Len = len;
notifyApp = SIMPLEPROFILE_CHAR7;
}
break;
// GAP GATT Attributes
#define DEVICE_NAME "SmartRF"
typedef struct
{
uint8 name[GAP_DEVICE_NAME_LEN]; //本机设备名称
uint8 pwm[4];
}SYS_CONFIG;
SYS_CONFIG sys_set;
void Init_Paramter(void)
{
int8 ret8 = osal_snv_read(0x80, sizeof(SYS_CONFIG), &sys_set);
if(NV_OPER_FAILED == ret8)
{
// 未初始化
osal_memset(&sys_set, 0, sizeof(SYS_CONFIG));
sprintf((char*)sys_set.name, DEVICE_NAME); // 默认设备名称
// 4路 pwm 设置, 相对于0xFF 的占空比
sys_set.pwm[0] = 0x10;
sys_set.pwm[1] = 0x20;
sys_set.pwm[2] = 0x30;
sys_set.pwm[3] = 0x40;
osal_snv_write(0x80, sizeof(SYS_CONFIG), &sys_set); // 写所有参数
}
}
Init_Paramter();
// GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
{
uint8 scanRspDataLen = 11 + osal_strlen((char*)sys_set.name);
uint8 scanRspDataTemp[B_MAX_ADV_LEN];
uint8 i = 0;
scanRspDataTemp[i++] = 1 + osal_strlen((char*)sys_set.name);
scanRspDataTemp[i++] = GAP_ADTYPE_LOCAL_NAME_COMPLETE; // length of this data
osal_memcpy(&scanRspDataTemp[i], sys_set.name, osal_strlen((char*)sys_set.name));
i = 2 + osal_strlen((char*)sys_set.name);
// connection interval range
scanRspDataTemp[i++] = 0x05, // length of this data
scanRspDataTemp[i++] = GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
scanRspDataTemp[i++] = LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ); // 100ms
scanRspDataTemp[i++] = HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL );
scanRspDataTemp[i++] = LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ); // 1s
scanRspDataTemp[i++] = HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL );
// Tx power level
scanRspDataTemp[i++] = 0x02; // length of this data
scanRspDataTemp[i++] = GAP_ADTYPE_POWER_LEVEL;
scanRspDataTemp[i++] = 0; // 0dBm
GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, scanRspDataLen, scanRspDataTemp );
}
case SIMPLEPROFILE_CHAR7_UUID:
HalLcdWriteString( "Char 7 read", HAL_LCD_LINE_3 );
len = osal_strlen((char*)sys_set.name);
osal_memcpy(newValue, sys_set.name, len);
break;
case SIMPLEPROFILE_CHAR7: // 收到 CHAR7 的数据
SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR7, &newValue, &returnBytes);
osal_memset(sys_set.name, 0, sizeof(sys_set.name));
osal_memcpy(sys_set.name, newValue, returnBytes);
osal_snv_write(0x80, sizeof(SYS_CONFIG), &sys_set); // 写所有参数
HalLcdWriteString( "Char 7 Set", HAL_LCD_LINE_4 );
break;