蓝牙 ==== 蓝牙相关AT指令 -------------- 打开/关闭 蓝牙指令:AT+BTCOMM=ENABLE ------------------------------------ 描述: ​ 此命令用于打开/关闭蓝牙 语法规则: ======== ================ ========== 命令类型 语法 返回 ======== ================ ========== 设置命令 AT+BTCOMM= +BTCOMM:OK ======== ================ ========== 参数定义: ====== ======== ======= ============ 参数 定义 取值 对取值的说明 ====== ======== ======= ============ 打开蓝牙 ENABLE 字符串参数 \ 关闭蓝牙 DISABLE 字符串参数 ====== ======== ======= ============ 举例: :: AT+BTCOMM=ENABLE +BTCOMM:OK OK AT+BTCOMM=DISABLE +BTCOMM:OK OK 如果失败会返回 +BTCOMM:ERR=error 设置广播参数:\ **AT+BLEADV=SETADVPARAM** ----------------------------------------- 描述: 此命令,用于设置广播参数,有6个或8个参数,当广播类型是定向广播时,参数为8个。 语法规则: ======== ================================= ========== 命令类型 语法 返回 ======== ================================= ========== 设置命令 AT+BLEADV=SETADVPARAM,para1,para2 +BLEADV:OK ======== ================================= ========== 参数定义: +---------+-------------------+---------+-----------------------------+ | 参数 | 定义 | 取值 | 对取值的说明 | +=========+===================+=========+=============================+ | Para1 | 最小广播间隔, | 20ms | 数字 | | | 单位0.625ms(必选) | ~10.24s | | +---------+-------------------+---------+-----------------------------+ | Para2 | 最大广播间隔, | 20ms | 数字 | | | 单位0.625ms(必选) | ~10.24s | | +---------+-------------------+---------+-----------------------------+ | Para3 | 广播类型 | 1 -4 | 数字 | | | (必选):0: | | | | | Connectable | | | | | undirected | | | | | advertising | | | | | (ADV_IND)1: | | | | | Connectable high | | | | | duty cycle | | | | | directed | | | | | advertising2: | | | | | Scannable | | | | | undirected | | | | | advertising | | | | | (ADV_SCAN_IND)3: | | | | | Non connectable | | | | | undirected | | | | | advertising(A | | | | | DV_NONCONN_IND)4: | | | | | Connectable low | | | | | duty cycle | | | | | directed | | | | | advertising | | | +---------+-------------------+---------+-----------------------------+ | Para4 | 广播本地地 | 0-2 | 数字 | | | 址类型(必选):0: | | | | | Public Device | | | | | Address1: Random | | | | | Device Address | | | +---------+-------------------+---------+-----------------------------+ | Para5 | 定向地 | 0-1 | 数字 | | | 址类型(可选):0: | | | | | Public Device | | | | | Address1: Random | | | | | Device Address | | | +---------+-------------------+---------+-----------------------------+ | Para6 | 定向地址(可选) | 1 | 数字 | +---------+-------------------+---------+-----------------------------+ | Para7 | 广播channel | 1-7 | 数字 | | | map,3个 | | | | | bit,分别对应37, | | | | | 38, 39信道 | | | | | (必选) | | | +---------+-------------------+---------+-----------------------------+ | Para8 | 广播过 | 0-3 | 数字 | | | 滤策略(必选):0: | | | | | Process scan and | | | | | connection | | | | | requests from all | | | | | devices.1: | | | | | Process | | | | | connection | | | | | requests from all | | | | | devices and only | | | | | scanrequests from | | | | | devices that are | | | | | in the White | | | | | List.2: Process | | | | | scan requests | | | | | from all devices | | | | | and only | | | | | c | | | | | onnectionrequests | | | | | from devices that | | | | | are in the White | | | | | List.3: Process | | | | | scan and | | | | | connection | | | | | requests only | | | | | from devices | | | | | inthe White List | | | +---------+-------------------+---------+-----------------------------+ 举例: .. code:: c AT+BLEADV=SETADVPARAM, 96,128,0,0,7,0 +BLEADV:OK OK 如果失败会返回 + BLEADV:ERR=error 设置BLE设备名称:\ **AT+BLECOMM=SETNAME** ----------------------------------------- | 描述: 此命令,用于设置BLE设备名称,参数是设备名称,最长29 byte | 语法规则: 语法规则: ======== ================================= ========== 命令类型 语法 返回 ======== ================================= ========== 设置命令 AT+BLECOMM=SETNAME, para1=value1 +BTCOMM:OK ======== ================================= ========== 参数定义: +-------+-------------------------------------+------+--------------+ | 参数 | 定义 | 取值 | 对取值的说明 | +=======+=====================================+======+==============+ | para1 | 参数为设备名,最长不能超过29字节; | - | ASCII | | | 只允许使用英文、 数字、 下划 线; | | | +-------+-------------------------------------+------+--------------+ 举例: .. code:: c AT+BLECOMM=SETNAME,AIRM2M_LUATBT +BLEADV:OK AT+BLECOMM=NAME? +BLECOMM: NAME=AIRM2M_LUATBT 如果失败会返回 +BLECOMM:ERR=error 打开/关闭 蓝牙广播指令:AT+BLEADV=SETADVENABLE ---------------------------------------------- 描述: ​ 此命令,用于开关广播,有一个参数 语法规则: ======== ========================== ========== 命令类型 语法 返回 ======== ========================== ========== 设置命令 AT+BLEADV=SETADVENABLE, +BLEADV:OK ======== ========================== ========== 参数定义: ==== ======== ==== ============ 参数 定义 取值 对取值的说明 ==== ======== ==== ============ 关闭广播 0 数字 \ 打开广播 1 数字 ==== ======== ==== ============ 举例: .. code:: c AT+BLEADV=SETADVENABLE,0 +BLEADV:OK OK AT+BLEADV=SETADVENABLE,1 +BLEADV:OK OK 如果失败会返回 + BLEADV:ERR=error 设置广播数据:\ **AT+BLEADV=SETADVDATA** ---------------------------------------- 描述: ​ 此命令,用于设置广播数据,有2个参数 语法规则: ======== ====================================== ========== 命令类型 语法 返回 ======== ====================================== ========== 读取命令 **AT+ BLEADV= SETADVDATA,para1,para2** +BLEADV:OK ======== ====================================== ========== 参数定义: ===== ======== Para1 数据长度 ===== ======== Para2 数据 ===== ======== 举例: .. code:: c AT+BLEADV=SETADVDATA,5,04ff010203 +BLEADV:OK 如果失败会返回: + BLEADV:ERR=error 给中心设备(手机)发送通知指令:AT+SPBTCTRL=GATTSETNOTIFY ------------------------------------------------------- 描述: ​ 此命令,用于给中心设备(手机)发送通知 语法规则: ======== =============================== ==== 命令类型 语法 返回 ======== =============================== ==== 设置命令 AT+SPBTCTRL=GATTSETNOTIFY, OK ======== =============================== ==== 参数定义: ===== ============ ===== ============ 参数 定义 取值 对取值的说明 ===== ============ ===== ============ 具体通知数据 字符串参数 ===== ============ ===== ============ 举例: .. code:: c AT+SPBTCTRL=GATTSETNOTIFY,"1111" OK 如果失败会返回: + SPBTCTRL:ERR=error 查询蓝牙使能状态:AT+BTCOMM=STATE? ---------------------------------- 语法规则: ======== ================ ================= 命令类型 语法 返回 ======== ================ ================= 查询命令 AT+BTCOMM=STATE? +BTCOMM:STATE= ======== ================ ================= 参数定义: ==== ============ ======= ============ 参数 定义 取值 对取值的说明 ==== ============ ======= ============ 当前已经使能 enable 字符串参数 \ 当前未使能 disable 字符串参数 ==== ============ ======= ============ 举例: .. code:: c AT+BTCOMM=STATE? +BTCOMM:STATE=disable AT+BTCOMM=STATE? +BTCOMM:STATE=disable 如果失败会返回: + BTCOMM:ERR=error 蓝牙测试/使用流程 ----------------- 1. 下载手机蓝牙测试工具 `nrf connect `__\ (必须) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2. 通过串口发送指令 AT+BTCOMM=ENABLE (必须) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: AT+BTCOMM=ENABLE +BTCOMM:OK 3.设置广播参数 (非必须,需要在广播关闭状态下发送) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: AT+BLEADV=SETADVPARAM,96,128,0,0,7,0 +BLEADV:OK ####4.设置广播模块名称(非必须,需要在广播关闭状态下发送) :: AT+BLECOMM=SETNAME,AIRM2M +BLECOMM:OK 5.设置广播数据(非必须,需要在广播关闭状态下发送) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: AT+BLEADV=SETADVDATA,5,04ff010203 +BLEADV:OK *此处数据段(04ff010203 )含义是长度为04,标识为manufacturer specific data(ff)的数据,详情请见BLE广播协议* 6.打开广播 (必须) ^^^^^^^^^^^^^^^^^^^ :: AT+BLEADV=SETADVENABLE,1 +BLEADV:OK 7.连接蓝牙(必须) ^^^^^^^^^^^^^^^^^^ ​ i.打开第一步下载的nrf connect 安卓应用 ​ ii.查看蓝牙和广播信息(当前信息是根据上述AT来的,如果字节修改了其他值则看到的不一样) .. image:: http://openluat-luatcommunity.oss-cn-hangzhou.aliyuncs.com/images/20200829200136854_寻找蓝牙.png 图(1).寻找蓝牙和功能点介绍 .. image:: http://openluat-luatcommunity.oss-cn-hangzhou.aliyuncs.com/images/20200829200151237_广播数据.png 图(2).广播内容介绍 .. image:: http://openluat-luatcommunity.oss-cn-hangzhou.aliyuncs.com/images/20200829200205946_蓝牙服务和特征值描述.png 图(3).当前蓝牙服务介绍 8.测试蓝牙 ^^^^^^^^^^ ​ i.手机给蓝牙发送数据 ​ |image1| ​ 图(4).手机给模块发送数据 ​ 通过上述 图(3) 特征值为fffe 的特征发送数据(4321),串口数据会吐出 :: 04 03 02 01 ii.模块给中心设备(手机)发送数据 :: AT+SPBTCTRL=GATTSETNOTIFY,12345 OK 手机收到 |image2| 图(5).手机收到模块发送的数据 ​ .. |image1| image:: http://openluat-luatcommunity.oss-cn-hangzhou.aliyuncs.com/images/20200829200233910_手机收到模块发送的数据.png .. |image2| image:: http://openluat-luatcommunity.oss-cn-hangzhou.aliyuncs.com/images/20200829200301511_手机收到模块发送的数据.png