【cc2541历程】APP更改蓝牙设备名称

一、简介

本文介绍如何在SimpleBLEPeripheral工程中,以char7为例。实现用手机端APP更改蓝牙设备名称.

二、实验平台

编译环境:IAR820.02

协议栈:BLE-CC254x-1.40

代码查看器:Source Insight 3.5

硬件平台:SmartRf 开发板

安卓APP平台: SmartRf

三、编写声明

作者:爱已oО欠费

相关CC2541群:606444519

淘宝小店:https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4023-16306486995.5.hdKUUO&id=548576956486
四、实验前提

请完成以下博文:

【cc2541历程】ds18b20发送数值到App:http://blog.csdn.net/wangjiawu7/article/details/72648402

五、硬件原理图

暂无

六、实验步骤

1.增加char7宏定义(simpleGATTprofile.h中CONSTANTS段部分))
#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 输出


2.增加char6的UUID(simpleGATTprofile.c中GLOBAL VARIABLES段部分)
// Characteristic 7 UUID: 0xFFF7
CONST uint8 simpleProfilechar7UUID[ATT_BT_UUID_SIZE] =
{ 
  LO_UINT16(SIMPLEPROFILE_CHAR7_UUID), HI_UINT16(SIMPLEPROFILE_CHAR7_UUID)
};


3.增加char7的配置属性(simpleGATTprofile.c中Profile Attributes - variables段)
// 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";


4.修改属性表.

1)修改属性表宏定义( simpleGATTprofile.c中 CONSTANTS部分 ):
#define SERVAPP_NUM_ATTR_SUPPORTED        		 	27//24//20//17


2)修改属性表(simpleGATTprofile.c中simpleProfileAttrTbl部分):
      // 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 
      },


5.修改特征值参数
1)增加char7数值可设置处理(simpleGATTprofile.c中SimpleProfile_SetParameter部分):
    case SIMPLEPROFILE_CHAR7:
    case SIMPLEPROFILE_CHARA:
      ret = bleInvalidRange;
      break;


2)增加char7数值可获取处理(simpleGATTprofile.c中SimpleProfile_GetParameter部分):
    case SIMPLEPROFILE_CHAR7:
      VOID osal_memcpy( value, simpleProfileChar7, simpleProfileChar7Len );
      *returnBytes = simpleProfileChar7Len;
      break;  


6.增加修改特征值读写
1)增加char7数值读取处理(simpleGATTprofile.c中simpleProfile_ReadAttrCB部分):
	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;


2)增加char7数值写入处理(simpleGATTprofile.c中simpleProfile_WriteAttrCB部分)
	      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;    



7.修改应用层
1)修改设备初始化名称(simpleBLEPeripheral.c中GAP GATT Attributes部分)
// GAP GATT Attributes
#define DEVICE_NAME  "SmartRF"


2)修改结构体初始化(simpleBLEPeripheral.c中 Simple GATT Profile Callbacks)
typedef struct 
{
    uint8 name[GAP_DEVICE_NAME_LEN];      //本机设备名称

    uint8 pwm[4];              
}SYS_CONFIG;

SYS_CONFIG sys_set;



3)初始化相关参数(simpleBLEPeripheral.c中GATT Profile Callbacks部分)
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);    // 写所有参数
    }
}



4)在初始化函数中添加函数(simpleBLEPeripheral.c中SimpleBLEPeripheral_Init)
   Init_Paramter();


5)添加设置广播相关参数simpleBLEPeripheral.c中SimpleBLEPeripheral_Init中的Role Parameters部分
//    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 );

}


6)修改应用层读取函数(simpleBLEPeripheral.c中simpleProfileReadConfig部分)
      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;


7)修改应用层写入函数(simpleBLEPeripheral.c中simpleProfileChangeCB部分)
    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;


七、实验结果

在APP端点击修改设备名称,然后重启硬件设备。广播到的设备名称已改变,实验成功。






你可能感兴趣的:(【cc2541历程】APP更改蓝牙设备名称)