1.目的
把官方的代码移植到sdk10.0版本
2.分析
由于官方的版本过低,移植到高版本
3.平台:
协议栈版本:SDK10.0.0
编译软件:keil 5.12
硬件平台:nrf51822最小系统
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4
4.步骤
需要准备的资料,
一个是微信官方代码,一个是nrfSDK
微信下载地址:http://iot.weixin.qq.com/wiki/new/index.html?page=6-1
首先,添加几个文件夹:
文件夹的内容分别实在微信的对应名字的文件copy出来的,把他们加到SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4工程里面,并添加对应的路径
文件夹对应的内容为:
在main.c里面添加
static ble_wechat_t m_ble_wechat; //¶¨Òå΢ÐÅ·þÎñ
uint8_t m_addl_adv_manuf_data[BLE_GAP_ADDR_LEN];
#define COMPANY_IDENTIFIER 0x0056
ble_evt_dispatch()函数变为如下:
/**@brief Function for dispatching a S110 SoftDevice event to all modules with a S110 SoftDevice
* event handler.
*
* @details This function is called from the S110 SoftDevice event interrupt handler after a S110
* SoftDevice event has been received.
*
* @param[in] p_ble_evt S110 SoftDevice event.
*/static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
ble_conn_params_on_ble_evt(p_ble_evt);
//ble_nus_on_ble_evt(&m_nus, p_ble_evt);
on_ble_evt(p_ble_evt);
ble_advertising_on_ble_evt(p_ble_evt);
bsp_btn_ble_on_ble_evt(p_ble_evt);
ble_wechat_on_ble_evt(&m_ble_wechat, p_ble_evt, m_mpbledemo2_handler); //Ìí¼ÓÍþÐÅ·þÎñʼþ
m_mpbledemo2_handler->m_data_on_ble_evt_func(&m_ble_wechat,p_ble_evt); //
}
services_init()函数变为:
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
uint32_t err_code;
// ble_nus_init_t nus_init;
//
// memset(&nus_init, 0, sizeof(nus_init));
// nus_init.data_handler = nus_data_handler;
//
// err_code = ble_nus_init(&m_nus, &nus_init);
// APP_ERROR_CHECK(err_code);
err_code = ble_wechat_add_service(&m_ble_wechat);
APP_ERROR_CHECK(err_code);
err_code = ble_wechat_add_characteristics(&m_ble_wechat);
APP_ERROR_CHECK(err_code);
}
advertising_init()函数变为:
**@brief Function for initializing the Advertising functionality.
*/
static void advertising_init(void)
{
uint32_t err_code;
ble_advdata_t advdata;
//ble_advdata_t scanrsp;
// uint32_t err_code;
//ble_advdata_t advdata;
/**< LE General Discoverable Mode, BR/EDR not supported. */
uint8_t flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
ble_uuid_t adv_uuids[] =
{
{BLE_UUID_WECHAT_SERVICE, BLE_UUID_TYPE_BLE}
};
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.include_appearance = false;
advdata.flags = flags;
advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
advdata.uuids_complete.p_uuids = adv_uuids;
ble_advdata_manuf_data_t manuf_data;
manuf_data.company_identifier = COMPANY_IDENTIFIER;
manuf_data.data.size = sizeof(m_addl_adv_manuf_data);
manuf_data.data.p_data = m_addl_adv_manuf_data;
advdata.p_manuf_specific_data = &manuf_data;
err_code = ble_advdata_set(&advdata, NULL);
APP_ERROR_CHECK(err_code);
// Initialize advertising parameters (used when starting advertising)
//memset(&m_adv_params, 0, sizeof(m_adv_params));
// Build and set advertising data
// Build advertising data struct to pass into @ref ble_advertising_init.
// memset(&advdata, 0, sizeof(advdata));
// advdata.name_type = BLE_ADVDATA_FULL_NAME;
// advdata.include_appearance = false;
// advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
// memset(&scanrsp, 0, sizeof(scanrsp));
// scanrsp.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
// scanrsp.uuids_complete.p_uuids = m_adv_uuids;
ble_adv_modes_config_t options = {0};
options.ble_adv_fast_enabled = BLE_ADV_SLOW_ENABLED;
options.ble_adv_fast_interval = APP_ADV_INTERVAL;
options.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS;
err_code = ble_advertising_init(&advdata, NULL, &options, on_adv_evt, NULL);
APP_ERROR_CHECK(err_code);
}
添加如下代码:
/****************************************************************************
? ?: get_mac_addr()
? ?: »ñµÃÀ¶ÑÀµÄmacµØÖ·
????: void
????: ?
****************************************************************************/
void get_mac_addr(uint8_t *p_mac_addr)
{
uint32_t error_code;
ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
error_code = sd_ble_gap_address_get(p_mac_addr_t);
APP_ERROR_CHECK(error_code);
#ifdef CATCH_LOG
printf("\r\n error:%d",error_code);
printf("\r\n get mac addr");
#endif
uint8_t *d = p_mac_addr_t->addr; for ( uint8_t i = 6; i >0;)
{
i--;
p_mac_addr[5-i]= d[i];
#ifdef CATCH_LOG
printf ( ":%02x", d[i]);
#endif
}
#ifdef CATCH_LOG
putchar ( '\n' );
#endif
free(p_mac_addr_t);
p_mac_addr_t = NULL;
}
/****************************************************************************
? ?: data_handler_init()
? ?: »Øµ÷º¯Êý
????: void
????: ?
****************************************************************************/
void data_handler_init(data_handler** p_data_handler, uint8_t product_type)
{
if (*p_data_handler == NULL)
{
*p_data_handler = get_handler_by_type(product_type);
}
}
/****************************************************************************
? ?: register_all_products()
? ?: ×¢²á»Øµ÷º¯Êý
????: void
????: ?
****************************************************************************/
//function for register all products
void register_all_products(void) {
REGISTER(mpbledemo2);
}
主函数如下:
/**@brief Application main function.
*/
int main(void)
{
uint32_t err_code;
bool erase_bonds;
uint8_t start_string[] = START_STRING;
// Initialize.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
uart_init();
buttons_leds_init(&erase_bonds);
ble_stack_init();
//微信处理
get_mac_addr(m_addl_adv_manuf_data);
register_all_products();
data_handler_init(&m_mpbledemo2_handler, PRODUCT_TYPE_MPBLEDEMO2);
gap_params_init();
services_init();
advertising_init();
conn_params_init();
printf("%s",start_string);
//
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
power_manage();
m_mpbledemo2_handler->m_data_main_process_func(&m_ble_wechat);
}
}
打开#include "ble_wechat_service.h"文件,屏蔽
OK。。。待测试。