一 前言
这里的代码是在原厂基础上修改了不少。虽然代码复杂了不少,但是逻辑也清晰了不少。
二 广播协议
想要熟悉并修改ble的广播协议和内容,请查阅结构体:
static const attribute_t my_Attributes[]
比如,假如要上报电压值,可以在里面添加
{0,ATT_PERMISSIONS_READ,2,sizeof(my_batCharVal),(u8*)(&my_characterUUID), (u8*)(my_batCharVal), 0}, //prop
该广播信息的填写如下所示:
//// Battery attribute values static const u8 my_batCharVal[5] = { CHAR_PROP_READ | CHAR_PROP_NOTIFY, U16_LO(BATT_LEVEL_INPUT_DP_H), U16_HI(BATT_LEVEL_INPUT_DP_H), U16_LO(CHARACTERISTIC_UUID_BATTERY_LEVEL), U16_HI(CHARACTERISTIC_UUID_BATTERY_LEVEL) };
三 连接和断开
连接成功的接口如下所示:
case APP_BLE_EVENT_CONNECTED: b_ble_connected = ON; LOG_INFO("Connected!!! ble_state:%d ",b_ble_connected); LOG_INFO("e: %d p:%d n: %d", param->e, param->p, param->n);
断开的接口如下所示:
case APP_BLE_EVENT_DISCONNECTED: b_ble_connected = OFF; b_rx_notify_enable = OFF; LOG_INFO("DisConnected Reason: 0x%02x ble_state:%d ", param->p,b_ble_connected); att_mtu_size = DEFAULT_ATT_MTU_SIZE;
四 数据收发
数据接收的接口函数:
static int parse_xp_message(XpMessage* message, uint8_t* data, uint16_t len) { uint8_t* p = NULL; p = data; set_xp_header(message, data, &p); LOG_DEBUG("%s Info: ble received data:", __func__);
数据发送的接口函数:
app_ble_send_data_by_notification(buff, length);
五 软硬件链接: