gpt4 book ai didi

javascript - 如何创建到 Telit BLE 模块的终端 I/O 连接?

转载 作者:行者123 更新时间:2023-11-30 06:17:19 26 4
gpt4 key购买 nike

我正在为 Android 平板电脑编写一个渐进式网络应用程序,它应该能够通过 BLE 连接读取和写入具有嵌入式 Telit BLE 模块的设备。

我能够打开 BLE 连接并发现服务和特征。我无法使用 Telit 的终端 I/O (TIO) 协议(protocol)通过 BLE 建立连接。

我的远程(服务器)设备是 Falcom Fox3 跟踪器。建立连接后,可以从 Fox3 串行端口读取通知事件。这已通过使用 Telit 的 android 终端应用程序连接到 Fox3 成功测试:https://play.google.com/store/apps/details?id=com.telit.tiosample

我整理了一个简短的函数,它应该通过 BLE 连接、建立 TIO 连接并在监听来自服务器的传入数据之前请求 UART 积分。

我的代码基于 Smashing Magazine 中的一个简单脚本: https://www.smashingmagazine.com/2019/02/introduction-to-webbluetooth/

接下来启动 TIO 连接的过程在 Telit 的 Terminal I/O Profile Client Implementation Guide 中给出。

the Terminal I/O connection setup consists of the following steps:

  • The Terminal I/O client scans for Bluetooth Low Energy devices advertising the Terminal I/O service.
  • The Terminal I/O client establishes a Bluetooth Low Energy GATT connection to a detected Terminal I/O server.
  • The Terminal I/O client performs a service discovery on the Terminal I/O server.
  • For the retrieved Terminal I/O service, the Terminal I/O client performs a characteristics discovery.
  • The Terminal I/O client subscribes to indications of the UART credits TX characteristic (see 7.4).
  • The Terminal I/O client subscribes to notifications of the UART data TX characteristic (see 7.2).
  • The Terminal I/O client transmits initial UART credits to the server (see 7.5).
  • Once the Terminal I/O client has received the response for the transmitted UART credits, the Terminal I/O connection is considered established and indications for the UART credits TX characteristic and notifications for the UART data characteristic shall be expected at any time.

The order of the connection setup sequence is mandatory.

我的代码如下,其中log是一个输出到屏幕的函数。

const SERVICE_UUID         = "0000fefb-0000-1000-8000-00805f9b34fb";
const UART_RX_UUID = "00000001-0000-1000-8000-008025000000";
const UART_TX_UUID = "00000002-0000-1000-8000-008025000000";
const UART_RX_CREDITS_UUID = "00000003-0000-1000-8000-008025000000";
const UART_TX_CREDITS_UUID = "00000004-0000-1000-8000-008025000000";

async function tio_connect() {
let device = await navigator.bluetooth.requestDevice({
filters: [{ namePrefix: 'FOX' }],
optionalServices: [SERVICE_UUID]
});
log(" - Connecting<br>");
let server = await device.gatt.connect();
log(" - Getting Primary Service<br>");
let service = await server.getPrimaryService(SERVICE_UUID);
log(" - Subscribing to tx credits<br>");
let tx_credits = await service.getCharacteristic(UART_TX_CREDITS_UUID);
log(" - Subscribing to tx data<br>");
let tx_data = await service.getCharacteristic(UART_TX_UUID);
log(" - Requesting credits<br>");
tx_credits.writeValue(new Uint8Array([255]));
log(" - Starting listener<br>");
tx_data.addEventListener('characteristicvaluechanged', e => {log (e.value)});
tx_data.startNotifications();
}

这运行没有错误,似乎在我的客户端 android 设备上建立了蓝牙连接。我希望服务器响应连接、触发事件并将其报告回来。没有这样的连接事件发生。

我是网络蓝牙的新手,对 JavaScript 有点生疏,所以不确定我是否使用了正确的调用 - 特别是“订阅”。如果有人能澄清在这种情况下订阅涉及什么,那肯定有助于我的理解。

编辑:一旦弄清楚终端 I/O 连接指令如何转换为 JS,我就能够运行连接。

我这样执行了这些步骤:“对于检索到的终端 I/O 服务,终端 I/O 客户端执行特征发现。”

let tx_credits = await service.getCharacteristic(UART_TX_CREDITS_UUID)
let tx_data = await service.getCharacteristic(UART_TX_UUID)
let rx_credits = await service.getCharacteristic(UART_RX_CREDITS_UUID)
let rx_data = await service.getCharacteristic(UART_RX_UUID)

“终端 I/O 客户端订阅 UART 信用 TX 特性的指示(参见 7.4)。”

await tx_credits.addEventListener('characteristicvaluechanged', e => {log ("<br>tx_credits: " + e.value)});
await tx_credits.startNotifications();

“Terminal I/O 客户端订阅 UART 数据 TX 特性的通知(见 7.2)。”

await tx_data.addEventListener('characteristicvaluechanged', e => {
for (i=1;tx_data.value.getUint8(i);i++){
log(String.fromCharCode(tx_data.value.getUint8(i)))
}
}
await tx_data.startNotifications();

“终端 I/O 客户端将初始 UART 信用传输到服务器(参见 7.5)。”

let rx_credit_level = await rx_credits.writeValue(new Uint8Array([255]))

最佳答案

您可能需要等待 writeValuestartNotifications

...
log(" - Requesting credits<br>");
await tx_credits.writeValue(new Uint8Array([255]));
log(" - Starting listener<br>");
tx_data.addEventListener('characteristicvaluechanged', e => {log (e.value)});
await tx_data.startNotifications();

关于javascript - 如何创建到 Telit BLE 模块的终端 I/O 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55374928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com