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

How does one create a Terminal I/O connection to a Telit BLE module?

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

我能够打开 BLE 连接并发现服务和特征。我无法使用 Telit 的终端 I/O (TIO) 协议通过 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 的终端 I/O 配置文件客户端实施指南中给出。

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 有点生疏,所以不确定我是否使用了正确的调用 - 特别是 'subscribing'。如果有人能澄清在这种情况下订阅涉及什么,那肯定有助于我的理解。

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

我这样执行了这些步骤: "For the retrieved Terminal I/O service, the Terminal I/O client performs a characteristics discovery."

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)

"The Terminal I/O client subscribes to indications of the UART credits TX characteristic (see 7.4)."

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

"The Terminal I/O client subscribes to notifications of the UART data TX characteristic (see 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();

"The Terminal I/O client transmits initial UART credits to the server (see 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();