大家好,欢迎来到IT知识分享网。
总述
BLE蓝牙在物联网中使用很广泛,许多WiFi物联网芯片都带有BLE蓝牙,天猫精灵找队友也是有使用BLE蓝牙广播实现的。BLE蓝牙缺点是不适合大数据传输的场景。
BLE蓝牙
BLE蓝牙主机开发基本包括:
- 广播数据的设置
- service的属性设置
广播
数据结构
数据结构,31个数据长度由n个以下结构构成:(除蓝牙5.0外广播数据最大31个字节长度)
Length | AD Type | AD Data |
---|---|---|
广播数据长度 | 广播数据类型 | 广播数据 |
HID设备广播数据
Length | AD Type | AD Data | Describe |
---|---|---|---|
0x02 | 0x01 | 0x05 | 设备标识,低功耗模式,有限发现 |
0x03 | 0x03 | 0x1218 | HID UUID |
0x0n | 0x09 | 设备名称 | 设备名称 |
0x03 | 0x19 | 键盘0xC103 鼠标0xC203 游戏手柄0xC303 | 外观ID |
struct bt_data limt_disc_data = (struct bt_data)BT_DATA_BYTES(BT_DATA_FLAGS,(BT_LE_AD_NO_BREDR | BT_LE_AD_LIMITED));
ad_discov[0] = limt_disc_data;
struct bt_data hid_uuid = (struct bt_data)BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x12, 0x18);
ad_discov[1] = hid_uuid;
bt_set_name("TinyKeyborad");
const char *name = bt_get_name();
struct bt_data data = (struct bt_data)BT_DATA(BT_DATA_NAME_COMPLETE,name, strlen(name));
ad_discov[2] = data;
struct bt_data appera = (struct bt_data)BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, 0xc1, 0x03);
ad_discov[3] = appera;
属性构成
BL602:
BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS), //0
/*返回数据 {0x11, 0x01, 0x00, 0x02}*/
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO,
BT_GATT_CHRC_READ,
BT_GATT_PERM_READ ,
hid_info_read_data,
NULL,
NULL), //1 2
/*返回键盘描述符*/
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP,
BT_GATT_CHRC_READ,
BT_GATT_PERM_READ ,
hid_map_read_data,
NULL,
NULL), // 3 4
/*暂时不处理*/
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT,
BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ ,
hid_ctrl_write_data,
NULL,
NULL), // 5 6
/*hid_ctrl_write_data 空函数,hid_descrtptor_read_data 返回 0x01 0x01 */
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT,
BT_GATT_CHRC_READ|BT_GATT_CHRC_NOTIFY,
BT_GATT_PERM_READ ,
NULL,
hid_ctrl_write_data,
NULL), // 7 8
BT_GATT_CCC(hid_ctrl_write_data, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, hid_descrtptor_read_data, NULL, NULL),
/*hid_ctrl_write_data 空函数,hid_descrtptor_read_data_1返回 0x01 0x02 */
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT,
BT_GATT_CHRC_READ|BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ|BT_GATT_PERM_WRITE ,
NULL,
hid_ctrl_write_data,
NULL),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, hid_descrtptor_read_data_1, NULL, NULL),
/*hid_mode_read_data 返回0x01*/
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_PROTOCOL_MODE,
BT_GATT_CHRC_READ|BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ ,
hid_mode_read_data,
NULL,
NULL),
/*hid_ctrl_write_data空函数hid_descrtptor_read_data_2 返回0x01 0x03*/
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT,
BT_GATT_CHRC_READ|BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ ,
NULL,
hid_ctrl_write_data,
NULL),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, hid_descrtptor_read_data_2, NULL, 1),
参考文章
https://www.bilibili.com/read/cv15067064
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11211.html