评测三 蓝牙透传(单向) 有时无线透传在无法布线时有很方便的效用,不妨试试蓝牙透传,效果如下:
具体是无线数据->串口数据,串口数据->无线数据,目前前者实现了,后者还有些问题未解决,
实现过程如下,基于工程demo/Bluetooth/peripheral_demo改成peripheral_uart_demo,同时目录下peripheral_uart_demo/gcc/defconfig文件里工程名也改成peripheral_uart_demo,然后引入串口读写独立接口即把demo/at_demo下的serial.c、serial.h、serial_debug.h复制到刚才peripheral_uart_demo工程下,由于要无线写以及串口写转无线,所以profile涉及到write_without_rsp和notify,具体配置为: 服务 特征 UUID 12345678-1234-5678-56789abcdef0 12345678-1234-5678-56789abcdef1 Properties Write no response/Notify
代码语言:javascript复制static struct bt_gatt_attr vnd_attrs[] = {
/* Vendor Primary Service Declaration */
BT_GATT_PRIMARY_SERVICE(&vnd_uuid),
BT_GATT_CHARACTERISTIC(&vnd_enc_uuid.uuid,
BT_GATT_CHRC_WRITE_WITHOUT_RESP | BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_WRITE,
NULL, write_without_rsp_vnd, &vnd_value),
BT_GATT_CCC(vnd_ccc_notify_changed, BT_GATT_PERM_READ|BT_GATT_PERM_WRITE),
};
写回调接口为:
代码语言:javascript复制/**********************vnd_write_cmd_uuid*****************************/
static ssize_t write_without_rsp_vnd(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags)
{
uint8_t *value = attr->user_data;
/* Write request received. Reject it since this char only accepts
* Write Commands.
*/
if (!(flags & BT_GATT_WRITE_FLAG_CMD)) {
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
if (offset len > sizeof(vnd_value)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
memset(value, 0, sizeof(vnd_value));
memcpy(value offset, buf, len);
serial_write(value offset, len);
*(value offset len) = '