|
|
@@ -6,32 +6,8 @@
|
|
|
@demo nimble
|
|
|
@tag LUAT_USE_NIMBLE
|
|
|
@usage
|
|
|
--- 本库当前支持Air101/Air103/ESP32/ESP32C3
|
|
|
--- 理论上支持ESP32C2/ESP32S2/ESP32S3,但尚未测试
|
|
|
-
|
|
|
--- 本库当前仅支持BLE Peripheral, 其他模式待添加
|
|
|
-sys.taskInit(function()
|
|
|
- -- 初始化nimble, 因为当仅支持作为主机,也没有其他配置项
|
|
|
- nimble.init("LuatOS-Wendal") -- 选取一个蓝牙设备名称
|
|
|
- sys.wait(1000)
|
|
|
-
|
|
|
- --local data = string.char(0x5A, 0xA5, 0x12, 0x34, 0x56)
|
|
|
- local data = "1234567890"
|
|
|
- while 1 do
|
|
|
- sys.wait(5000)
|
|
|
- -- Central端建立连接并订阅后, 可上报数据
|
|
|
- nimble.send_msg(1, 0, data)
|
|
|
- end
|
|
|
-end
|
|
|
-sys.subscribe("BLE_GATT_WRITE_CHR", function(info, data)
|
|
|
- -- Central端建立连接后, 可往设备写入数据
|
|
|
- log.info("ble", "Data Got", data:toHex())
|
|
|
-end)
|
|
|
-
|
|
|
--- 配合微信小程序 "LuatOS蓝牙调试"
|
|
|
--- 1. 若开发板无天线, 将手机尽量靠近芯片也能搜到
|
|
|
--- 2. 该小程序是开源的, 每次write会自动分包
|
|
|
--- https://gitee.com/openLuat/luatos-miniapps
|
|
|
+-- 本库当前支持Air101/Air103/ESP32/ESP32C3/ESP32S3
|
|
|
+-- 用法请查阅demo, API函数会归于指定的模式
|
|
|
*/
|
|
|
|
|
|
#include "luat_base.h"
|
|
|
@@ -70,6 +46,7 @@ ble_uuid_any_t ble_peripheral_write_uuid;
|
|
|
@return bool 成功与否
|
|
|
@usage
|
|
|
-- 参考 demo/nimble
|
|
|
+-- 本函数对所有模式都适用
|
|
|
*/
|
|
|
static int l_nimble_init(lua_State* L) {
|
|
|
int rc = 0;
|
|
|
@@ -97,6 +74,7 @@ static int l_nimble_init(lua_State* L) {
|
|
|
@return bool 成功与否
|
|
|
@usage
|
|
|
-- 仅部分设备支持,当前可能都不支持
|
|
|
+-- 本函数对所有模式都适用
|
|
|
*/
|
|
|
static int l_nimble_deinit(lua_State* L) {
|
|
|
int rc = 0;
|
|
|
@@ -142,6 +120,7 @@ static int l_nimble_server_deinit(lua_State* L) {
|
|
|
@return bool 成功与否
|
|
|
@usage
|
|
|
-- 参考 demo/nimble
|
|
|
+-- 本函数对peripheral/从机模式适用
|
|
|
*/
|
|
|
static int l_nimble_send_msg(lua_State *L) {
|
|
|
int conn_id = luaL_checkinteger(L, 1);
|
|
|
@@ -167,6 +146,7 @@ static int l_nimble_send_msg(lua_State *L) {
|
|
|
@return bool 成功与否
|
|
|
@usage
|
|
|
-- 参考 demo/nimble
|
|
|
+-- 本函数对central/主机模式适用
|
|
|
*/
|
|
|
static int l_nimble_scan(lua_State *L) {
|
|
|
int ret = luat_nimble_blecent_scan();
|
|
|
@@ -216,6 +196,8 @@ static int l_nimble_connok(lua_State *L) {
|
|
|
@usage
|
|
|
-- 参考 demo/nimble, 2023-02-25之后编译的固件支持本API
|
|
|
-- 必须在nimble.init()之前调用
|
|
|
+-- 本函数对peripheral/从机模式适用
|
|
|
+
|
|
|
-- 设置SERVER/Peripheral模式下的UUID, 支持设置3个
|
|
|
-- 地址支持 2/4/16字节, 需要二进制数据
|
|
|
-- 2字节地址示例: AABB, 写 string.fromHex("AABB") ,或者 string.char(0xAA, 0xBB)
|
|
|
@@ -251,6 +233,16 @@ static int l_nimble_set_uuid(lua_State *L) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+获取蓝牙MAC
|
|
|
+@api nimble.mac()
|
|
|
+@return string 蓝牙MAC地址,6字节
|
|
|
+@usage
|
|
|
+-- 参考 demo/nimble, 2023-02-25之后编译的固件支持本API
|
|
|
+-- 本函数对所有模式都适用
|
|
|
+local mac = nimble.mac()
|
|
|
+log.info("ble", "mac", mac and mac:toHex() or "Unknwn")
|
|
|
+*/
|
|
|
static int l_nimble_mac(lua_State *L) {
|
|
|
int rc;
|
|
|
uint8_t own_addr_type;
|
|
|
@@ -278,6 +270,36 @@ static int l_nimble_mac(lua_State *L) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+配置iBeacon的参数,仅iBeacon模式可用
|
|
|
+@api nimble.ibeacon(data, major, minor, measured_power)
|
|
|
+@string 数据, 必须是16字节
|
|
|
+@int 主版本号,默认2, 可选
|
|
|
+@int 次版本号,默认10,可选
|
|
|
+@int 名义功率, 默认0, 范围 -126 到 20
|
|
|
+@return bool 成功返回true,否则返回false
|
|
|
+@usage
|
|
|
+-- 参考 demo/nimble, 2023-02-25之后编译的固件支持本API
|
|
|
+-- 本函数对ibeacon模式适用
|
|
|
+nimble.ibeacon(data, 2, 10, 0)
|
|
|
+nimble.init()
|
|
|
+*/
|
|
|
+static int l_nimble_ibeacon(lua_State *L) {
|
|
|
+ size_t len = 0;
|
|
|
+ const char* data = luaL_checklstring(L, 1, &len);
|
|
|
+ if (len != 16) {
|
|
|
+ LLOGE("ibeacon data MUST 16 bytes, but %d", len);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ uint16_t major = luaL_optinteger(L, 2, 2);
|
|
|
+ uint16_t minor = luaL_optinteger(L, 3, 10);
|
|
|
+ int8_t measured_power = luaL_optinteger(L, 4, 0);
|
|
|
+
|
|
|
+ int rc = luat_nimble_ibeacon_setup(data, major, minor, measured_power);
|
|
|
+ lua_pushboolean(L, rc == 0 ? 1 : 0);
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
#include "rotable2.h"
|
|
|
static const rotable_Reg_t reg_nimble[] =
|
|
|
{
|
|
|
@@ -298,6 +320,9 @@ static const rotable_Reg_t reg_nimble[] =
|
|
|
{ "scan", ROREG_FUNC(l_nimble_scan)},
|
|
|
{ "connect", ROREG_FUNC(l_nimble_connect)},
|
|
|
|
|
|
+ // ibeacon广播模式
|
|
|
+ { "ibeacon", ROREG_FUNC(l_nimble_ibeacon)},
|
|
|
+
|
|
|
// 放一些常量
|
|
|
{ "STATE_OFF", ROREG_INT(BT_STATE_OFF)},
|
|
|
{ "STATE_ON", ROREG_INT(BT_STATE_ON)},
|