CC2541广播MAC地址

来源:互联网 发布:房地产大数据税收分析 编辑:程序博客网 时间:2024/04/30 01:27

一、简述
在实际BLE应用开发中,需要设备广播蓝牙标签MAC,比如苹果手机获取不到设备Mac,这是需要广播设备信息中附带MAC标识,外加自定义广播数据,如果考虑到存在安全隐患,因为广播包随时都可以被抓包。BLE协议栈自带加密解密API函数,可以通过调用此函数实现。如下:

//加密函数LL_Encrypt(uint8 *key,uint8 *plaintextData,uint8 *encryptedData ); //解密函数LL_EXT_Decrypt( uint8 *key,uint8 *encryptedData,uint8 *plaintextData );

这里主要介绍如何广播MAC地址,有两种实现方式:通过直接读寄存器和调用系统API函数,本篇介绍通过直接读寄存器方式获取MAC,通过调用API函数如下:

GAPRole_GetParameter(GAPROLE_BD_ADDR, MacAddress);

但是在系统初始化的时候只能通过读寄存器的方式实现,不能调用API获取MAC,此时MAC还未填入该变量中。
二、软件环境
协议栈版本:BLE-CC254x-1.4.0
编译器:IAR 8.20.2
三、步骤
1.读寄存器值,获取芯片MAC:
根据数据手册描述,从0x780e开始读6个字节就得到MAC地址了。
这里写图片描述
2.读取芯片MAC函数如下:

/**************************************************  Function name: Get_Mac  description:   Read the register value to get the Mac param[in]:     MacAddressReturn:        none  **************************************************/  static void Get_Mac(uint8 *MacAddress)         {    MacAddress[5] = XREG(0x780E);       MacAddress[4] = XREG(0x780F);      MacAddress[3] = XREG(0x7810);      MacAddress[2] = XREG(0x7811);                     MacAddress[1] = XREG(0x7812);      MacAddress[0] = XREG(0x7813);   }

3.修改广播数组,预留6个字节MAC位置:

// GAP - Advertisement data (max size = 31 bytes, though this is// best kept short to conserve power while advertisting)static uint8 advertData[] = {   // Flags; this sets the device to use limited discoverable  // mode (advertises for 30 seconds at a time) instead of general  // discoverable mode (advertises indefinitely)  0x02,   // length of this data  GAP_ADTYPE_FLAGS,  GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,  // three-byte broadcast of the data "1 2 3"  0x07,   // length of this data including the data type byte  GAP_ADTYPE_MANUFACTURER_SPECIFIC,      // manufacturer specific advertisement data type  0x20,  0x20,  0x20,  0x20,  0x20,  0x20,};

4.修改系统初始化函数SimpleBLEBroadcaster_Init:

uint8 MacAddress[B_ADDR_LEN];Get_Mac(MacAddress);osal_memcpy(advertData+5,MacAddress,6);GAPRole_SetParameter(GAPROLE_ADVERT_DATA,sizeof( advertData ), advertData );

5.通过SmartRF Flash Programmer查看芯片MAC地址:
这里写图片描述
6.通过SmartRF Packet Sniffer进行抓包查看广播数组中是否添加了MAC地址:
这里写图片描述
7.在手机端APP验证,确实获取到了MAC地址,验证程序可行:
这里写图片描述

本博文更新于2017-03-02 21:53。

1 0
原创粉丝点击