For research and development and testing BLE , often see the same name of the device, is extremely inconvenient, a large pile of equipment at the same time the Power will let colleagues do not know which device is what they really want to operate the target. Again millet bracelet, home has three millet bracelet, open the device search is all "MI", do not know which one to connect, so in the beginning of the use of the user to call the ring a few times, of course, this experience is also good. But in any case, as a developer, It's nice to be able to differentiate between a lot of BLE devices in Office. So the initiation lets the device name contain a unique identity-theMAC address.
Step one first, how to change the device name
It is extremely convenient to change the name in BLE, only the scan answer data and the GAP GATT name property need to be changed . Taking the simplebleperipheral project in TI BLE STACK 1.4.0 As an example,
The changes are:
Simplebleperipheral.c
Static Uint8 scanrspdata[];
Static Uint8 advertdata[];
The second step is how to generate a name that contains the device MAC address
eachBLEthe chip will have a6bytes ofMACaddress, read it and turn it intoASCIIcharacters, such as0x12 9a BCconverted to "123456789ABC", ifMACaddress is all written to the device name, it appears too long, after taking2byte address, or string "ABCD"can. Writes the prefix of the device name, along with the address string, to the scan answer data andGAP GATT NAMEproperty, when the device is running, we can then scan to see that its name has changed.
The third step is to achieve
The program can generate a self-unique name based on the MAC address, and use the initial program to modify the two mentioned in step one. The following code is a reference for generating a dynamic name. the simplebleperipheral_init () function call in the simplebleperipheral.c document bletask_ Blepara () function, and comment out the following call in the function.
Gaprole_setparameter (Gaprole_scan_rsp_data, sizeof (Scanrspdata), scanrspdata);
Gaprole_setparameter (Gaprole_advert_data, sizeof (Advertdata), advertdata);
Set the GAP characteristics
Ggs_setparameter (Ggs_device_name_att, Gap_device_name_len, attdevicename);
Reference code:
/**************************************************************************************************
Filename:simplebleotapatch.c
Editor:tome @ newbit Studio
Revised: $Date: 2015/8/11 11:20:02 +0800 $
Revision: $Revision: 00001 $
Description:
History:
Notes:
**************************************************************************************************/
/**************************************************************************************************
Includes
**************************************************************************************************/
#include "bcomdef.h"
#include "OSAL.h"
#include "Gatt.h"
#include "Gatt_profile_uuid.h"
#include "hci.h"
#include "Gap.h"
#include "Gapgattserver.h"
#include "Gattservapp.h"
#include "peripheral.h"
#include "Osal_snv.h"
#include "simpleBLEOTAPatch.h"
/**************************************************************************************************
TYPEDEF
**************************************************************************************************/
/**************************************************************************************************
CONSTANTS
**************************************************************************************************/
#define DEVICE_NAME "NB BOOT V3----"
#define Device_name_with_header "XNB BOOT V3----*----*----*---"
#define DEVICE_NAME_PLACEHOLDER_IDX 0//x is PLACEHOLDER
/**************************************************************************************************
GLOBAL VARIABLES
**************************************************************************************************/
/**************************************************************************************************
LOCAL veriable
**************************************************************************************************/
Gap-scan RSP data (max size = bytes)
Static Uint8 scanrspdata[31];
Gap-advertisement data (max size = bytes, though this is
Best kept-conserve power while advertisting)
Static Uint8 advertdata[31];
/**************************************************************************************************
FUNCTIONS decleration
**************************************************************************************************/
/**************************************************************************************************
FUNCTIONS
**************************************************************************************************/
/**************************************************************************************************
* @fn Bletask_scanrspdatainit
*
* @brief
*
* @param uint8 *dat
*
* @return int
**************************************************************************************************/
int Bletask_scanrspdatainit (uint8 *scanrsp)
{
Uint8 Dat[5] = {
Gap_adtype_slave_conn_interval_range,
Lo_uint16 (Default_desired_min_conn_interval),//100ms
Hi_uint16 (Default_desired_min_conn_interval),
Lo_uint16 (Default_desired_max_conn_interval),//1s
Hi_uint16 (Default_desired_max_conn_interval),
};
int len =
Utilserialization (SCANRSP, 0, 5, DAT);
Dat[0] = Gap_adtype_power_level;
DAT[1] = 0;
Len = utilserialization (SCANRSP, Len, 2, DAT);
return Len;
}
/**************************************************************************************************
* @fn Bletask_blepara
*
* @brief
*
* @param void
*
* @return void
**************************************************************************************************/
void Bletask_blepara (void)
{
int len = 0;
Len = Bletask_scanrspdatainit (scanrspdata);
Uint8 nvname[20];
Uint8 Rearnamelen = 4;
Uint8 *pname;
if (SUCCESS = = Osal_snv_read (Nvid_user_rearname, Len, Nvname))
{
PName = Nvname;
Rearnamelen = Osal_strlen ((char *) nvname);
}
Else
{
Uint8 Mac[6] = {1,2,3,4,5,6};
mac[5]=* (unsigned char *) (0X780E);
mac[4]=* (unsigned char *) (0x780f);
mac[3]=* (unsigned char *) (0x7810);
mac[2]=* (unsigned char *) (0x7811);
mac[1]=* (unsigned char *) (0x7812);
mac[0]=* (unsigned char *) (0x7813);
Utilprintmacaddress (Mac, (char*) nvname);
PName = nvname+8;
Rearnamelen = 4;
}
Uint8 name[] = Device_name_with_header;
uint8* Pfill = utilSearchChar2 (name, '-');
uint8* pEnd = osal_memcpy (Pfill, PName, Rearnamelen);
Uint8 length = Pend-name;
Uint8 length = sizeof (name);
NAME[LENGTH-5] = macstring[8];
NAME[LENGTH-4] = macstring[9];
NAME[LENGTH-3] = macstring[10];
Name[length-2] = macstring[11];
NAME[DEVICE_NAME_PLACEHOLDER_IDX] = Gap_adtype_local_name_complete;
Len = utilserialization (Scanrspdata, Len, length, name);
Should remove the placeholder! and decrease the length
Ggs_setparameter (Ggs_device_name_att, length-1, name+1);
Uint8 dat[10];
Advertise data
Dat[0] = gap_adtype_flags;
DAT[1] = Default_discoverable_mode | gap_adtype_flags_bredr_not_supported;
Len = utilserialization (advertdata, 0, 2, DAT);
Dat[0] = gap_adtype_16bit_more;//Some of the UUID ' s, but not all
DAT[1] = lo_uint16 (DEVINFO_SERV_UUID);
DAT[2] = hi_uint16 (DEVINFO_SERV_UUID);
DAT[3] = lo_uint16 (0XFEF9);
DAT[4] = hi_uint16 (0XFEF9);
Len = utilserialization (Advertdata, Len, 5, DAT);
Gaprole_setparameter (Gaprole_scan_rsp_data, sizeof (Scanrspdata), scanrspdata);
Gaprole_setparameter (Gaprole_advert_data, sizeof (Advertdata), advertdata);
}
/**************************************************************************************************
Copyright newbit Studio All rights reserved.
**************************************************************************************************/
Let the name of the BLE device contain the MAC address