评测三 蓝牙透传(单向)
有时无线透传在无法布线时有很方便的效用,不妨试试蓝牙透传,具体是无线数据->串口数据,串口数据->无线数据,目前前者实现了,后者还有些问题未解决,
实现过程如下,基于工程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 |
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),
};
写回调接口为:
/**********************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) = '\0';
printf("\r\nwrite_without_rsp_vnd");
return len;
}
串口转无线回调(有问题):
static void vnd_notify(void)
{
static uint8_t vnd[MAX_LONG_DATA];
uint16_t len=0;
if (!vnd_notif_enabled)
return;
printf("\r\nnotify\r\n");
serial_read(vnd_notify_value,len);
if(len>MAX_LONG_DATA || len==0)
return;
memcpy(vnd, vnd_notify_value, len);
printf("\r\nvnd_notify\r\n");
bt_gatt_notify(NULL, &vnd_svc.attrs[1], vnd, sizeof(vnd));
}
然后在bt_app_init函数里加入透传口UART1的初始化代码即可:
serial_init(SERIAL_UART_ID, 115200, UART_DATA_BITS_8, UART_PARITY_NONE,UART_STOP_BITS_1, 0);
serial_start();