前言
上篇已经简单分析了一下BLE协议栈的基本知识,今天就废话少说,直接先从最基础的点灯开始BLE蓝牙服务的开发。
实例分析
本文直接采用官方SDK的led例程修改,所以首先我们先来看看开发板的硬件连接:
从这里可以看出P0.18~P0.20分别对应LED0~LED2,当芯片输出高电平时LED将被点亮;
这时候我们来看下官方的LED驱动代码:
#define LEDS_OFF(leds_mask) do { NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
#define LEDS_ON(leds_mask) do { NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
这里可以知道官方的设置与我们实际是相反的,所以代码改为:
#define LEDS_OFF(leds_mask) do { NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
#define LEDS_ON(leds_mask) do { NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
接着,我们需要新建服务的驱动文件,如:ble_lbs.c和ble_lbs.h文件,这里我们直接分析官方的文件即可:
ble_lbs.h:
#define LBS_UUID_BASE {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, \ 0xDE, 0xEF, 0x12, 0x12, 0x00, 0x00, 0x00, 0x00} #define LBS_UUID_SERVICE 0x1523 #define LBS_UUID_LED_CHAR 0x1525
这里宏定义了私有服务的基础UUID和服务的UUID以及特征值的UUID;
问:这些UUID怎么来的呢?
答:使用nRFgo Studio生成基础UUID,例如生成的基础UUID是0000xxxx-1212-EFDE-1523-785FEABCD123,并且由于nrf51822是小端存储格式,所以定义时要倒置;
/** @brief LED Button Service init structure. This structure contains all options and data needed for * initialization of the service.*/ typedef struct { ble_lbs_led_write_handler_t led_write_handler; /**< Event handler to be called when the LED Characteristic is written. */ } ble_lbs_init_t; /**@brief LED Button Service structure. This structure contains various status information for the service. */ struct ble_lbs_s { uint16_t service_handle; /**< Handle of LED Button Service (as provided by the BLE stack). */ ble_gatts_char_handles_t led_char_handles; /**< Handles related to the LED Characteristic. */ ble_gatts_char_handles_t button_char_handles; /**< Handles related to the Button Characteristic. */ uint8_t uuid_type; /**< UUID type for the LED Button Service. */ uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack). BLE_CONN_HANDLE_INVALID if not in a connection. */ ble_lbs_led_write_handler_t led_write_handler; /**< Event handler to be called when the LED Characteristic is written. */ };
紧接着我们看到定义了两个结构体,一个是回调句柄结构体,一个是服务参数结构体;
/**@brief Function for initializing the LED Button Service. * * @param[out] p_lbs LED Button Service structure. This structure must be supplied by * the application. It is initialized by this function and will later * be used to identify this particular service instance. * @param[in] p_lbs_init Information needed to initialize the service. * * @retval NRF_SUCCESS If the service was initialized successfully. Otherwise, an error code is returned. */ uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init); /**@brief Function for handling the application's BLE stack events. * * @details This function handles all events from the BLE stack that are of interest to the LED Button Service. * * @param[in] p_lbs LED Button Service structure. * @param[in] p_ble_evt Event received from the BLE stack. */ void ble_lbs_on_ble_evt(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt);
最后,声明了两个API供上层应用调用。
ble_lbs.c:
/**@brief Function for handling the Connect event. * * @param[in] p_lbs LED Button Service structure. * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_connect(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt) { p_lbs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; } /**@brief Function for handling the Disconnect event. * * @param[in] p_lbs LED Button Service structure. * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_disconnect(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt) { UNUSED_PARAMETER(p_ble_evt); p_lbs->conn_handle = BLE_CONN_HANDLE_INVALID; } /**@brief Function for handling the Write event. * * @param[in] p_lbs LED Button Service structure. * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_write(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt) { ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; if ((p_evt_write->handle == p_lbs->led_char_handles.value_handle) && (p_evt_write->len == 1) && (p_lbs->led_write_handler != NULL)) { p_lbs->led_write_handler(p_lbs, p_evt_write->data[0]); } } void ble_lbs_on_ble_evt(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt) { switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: on_connect(p_lbs, p_ble_evt); break; case BLE_GAP_EVT_DISCONNECTED: on_disconnect(p_lbs, p_ble_evt); break; case BLE_GATTS_EVT_WRITE: on_write(p_lbs, p_ble_evt); break; default: // No implementation needed. break; } }
这里我们可以看到,文件中定义了4个API函数。分别是连接时,断开连接时以及写空中操作时调用的函数,我们主要留意下写操作怎么关联到处理回调函数即可。而ble_lbs_on_ble_evt这个函数则是作为应用层调用的接口,在事件派发函数中被调用。
接下来,我们还需要就是定义服务初始化函数以及为服务添加特征值的函数。
uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init) { uint32_t err_code; ble_uuid_t ble_uuid; // Initialize service structure. p_lbs->conn_handle = BLE_CONN_HANDLE_INVALID; p_lbs->led_write_handler = p_lbs_init->led_write_handler; // Add service. ble_uuid128_t base_uuid = {LBS_UUID_BASE}; err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type); if (err_code != NRF_SUCCESS) { return err_code; } ble_uuid.type = p_lbs->uuid_type; ble_uuid.uuid = LBS_UUID_SERVICE; err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle); if (err_code != NRF_SUCCESS) { return err_code; } // Add characteristics. err_code = button_char_add(p_lbs, p_lbs_init); if (err_code != NRF_SUCCESS) { return err_code; } err_code = led_char_add(p_lbs, p_lbs_init); if (err_code != NRF_SUCCESS) { return err_code; } return NRF_SUCCESS; }
static uint32_t led_char_add(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init) { ble_gatts_char_md_t char_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; memset(&char_md, 0, sizeof(char_md)); char_md.char_props.read = 1; char_md.char_props.write = 1; char_md.p_char_user_desc = NULL; char_md.p_char_pf = NULL; char_md.p_user_desc_md = NULL; char_md.p_cccd_md = NULL; char_md.p_sccd_md = NULL; ble_uuid.type = p_lbs->uuid_type; ble_uuid.uuid = LBS_UUID_LED_CHAR; memset(&attr_md, 0, sizeof(attr_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.rd_auth = 0; attr_md.wr_auth = 0; attr_md.vlen = 0; memset(&attr_char_value, 0, sizeof(attr_char_value)); attr_char_value.p_uuid = &ble_uuid; attr_char_value.p_attr_md = &attr_md; attr_char_value.init_len = sizeof(uint8_t); attr_char_value.init_offs = 0; attr_char_value.max_len = sizeof(uint8_t); attr_char_value.p_value = NULL; return sd_ble_gatts_characteristic_add(p_lbs->service_handle, &char_md, &attr_char_value, &p_lbs->led_char_handles); }
到这里,就基本完成服务驱动的代码。
应用层:
/**@brief Function for initializing services that will be used by the application. */ static void services_init(void) { uint32_t err_code; ble_lbs_init_t init; init.led_write_handler = led_write_handler; err_code = ble_lbs_init(&m_lbs, &init); APP_ERROR_CHECK(err_code); }
既然我们创建了私有服务,那么必然要在服务初始化里面将服务注册进去。需要注意的是,我们前面提到的写空中操作的处理回调函数也是要在这里注册,但现在我们还没完成回调函数的代码。所以,我们先把代码加上:
static void led_write_handler(ble_lbs_t * p_lbs, uint8_t led_state) { if (led_state) { LEDS_ON(LEDBUTTON_LED_PIN); } else { LEDS_OFF(LEDBUTTON_LED_PIN); } }
紧接着,就是将LED服务的事件处理函数添加到应用层的事件派发函数,这样当进行空中操作时就会触发事件派发,从而执行回调处理函数:
static void ble_evt_dispatch(ble_evt_t * p_ble_evt) { on_ble_evt(p_ble_evt); ble_conn_params_on_ble_evt(p_ble_evt); ble_lbs_on_ble_evt(&m_lbs, p_ble_evt); }
最后,虽然我们已经完成了服务的所有驱动代码以及应用层的注册,但是我们必须在广播里面加入服务的UUID。所以在广播初始化函数里面需要把服务的UUID加进去:
ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};
到这里,才真正完成任务。
结果验证
- 手机连接蓝牙,我们可以看到有一个新增的UUID为1523的服务,在其下包含一个UUID为1525的特制值;
- 1525特征值里包含了read和write两个属性;
- 点击write属性,往蓝牙写入0x01,LED0亮;往蓝牙写入0x00,LED0熄灭;
总结
通过这个实验,我们可以了解到如何添加私有服务。其他类似按键、串口等服务都可以按照这种方法添加,但如果工作中用到其实我们可以直接在官方的例程上修改即可,但原理我们还是要清楚。