本文主要记录bleprph工程的修改,目的是让其在收到APP端发送的数据后能回传回去
原始工程gatt_svr.c里面有个服务“59462f12-9543-9999-12c8-58b459a2712d”,有两个特性通道“5c3a659e-897e-45e1-b016-007107c96df6”和“5c3a659e-897e-45e1-b016-007107c96df7”, 其默认是配置如下
static const struct ble_gatt_svc_def gatt_svr_svcs[] = { { /*** Service: Security test. */ .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &gatt_svr_svc_sec_test_uuid.u, .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Random number generator. */ .uuid = &gatt_svr_chr_sec_test_rand_uuid.u, .access_cb = gatt_svr_chr_access_sec_test, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC, }, { /*** Characteristic: Static value. */ .uuid = &gatt_svr_chr_sec_test_static_uuid.u, .access_cb = gatt_svr_chr_access_sec_test, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC, }, { 0, /* No more characteristics in this service. */ } }, }, { 0, /* No more services. */ }, };
上面在APP发端数据时会弹出一个授权配对的对话框,其主要是原因是上面.flags使用了 ‘LE_GATT_CHR_F_WRITE_ENC’ 这个属性。现将属性修改如 下:一个通道负责收数据,另一通道负责发数据,同时去掉须授权
uint16_t gatt_svr_val_handle;
static const struct ble_gatt_svc_def gatt_svr_svcs[] = { { /*** Service: Security test. */ .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &gatt_svr_svc_sec_test_uuid.u, .characteristics = (struct ble_gatt_chr_def[]) { { /*** Characteristic: Random number generator. */ .uuid = &gatt_svr_chr_sec_test_rand_uuid.u, .access_cb = gatt_svr_chr_access_sec_test, .flags = BLE_GATT_CHR_F_NOTIFY,//BLE_GATT_CHR_F_READ ,| BLE_GATT_CHR_F_READ_ENC, .val_handle = &gatt_svr_val_handle, }, { /*** Characteristic: Static value. */ .uuid = &gatt_svr_chr_sec_test_static_uuid.u, .access_cb = gatt_svr_chr_access_sec_test, .flags = BLE_GATT_CHR_F_WRITE_NO_RSP,//BLE_GATT_CHR_F_READ | //BLE_GATT_CHR_F_WRITE, | BLE_GATT_CHR_F_WRITE_ENC, }, { 0, /* No more characteristics in this service. */ } }, }, { 0, /* No more services. */ }, };
注释掉函数gatt_svr_chr_write,主要是编译出错说此函数定义了但未使用。其次是对 gatt_svr_chr_access_sec_test 修改让其在收到数据后回传出去
/* static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len) { uint16_t om_len; int rc; om_len = OS_MBUF_PKTLEN(om); if (om_len < min_len || om_len > max_len) { return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; } rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); if (rc != 0) { return BLE_ATT_ERR_UNLIKELY; } return 0; } */ static int gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { const ble_uuid_t *uuid; int rc; uuid = ctxt->chr->uuid; /* Determine which characteristic is being accessed by examining its * 128-bit UUID. */ if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) { switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val, sizeof gatt_svr_sec_test_static_val); return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; case BLE_GATT_ACCESS_OP_WRITE_CHR: { struct os_mbuf *txom; txom = ble_hs_mbuf_from_flat(ctxt->om->om_data, ctxt->om->om_len); if (!txom) { return 0; } rc = ble_gattc_notify_custom(conn_handle, gatt_svr_val_handle, txom); }return rc; default: assert(0); return BLE_ATT_ERR_UNLIKELY; } } /* Unknown characteristic; the nimble stack should not have called this * function. */ assert(0); return BLE_ATT_ERR_UNLIKELY; }
最后使用bluelight 应用测试其功能如下:
——