如何创建与Telit BLE模块的终端I/O连接? [英] How does one create a Terminal I/O connection to a Telit BLE module?

查看:62
本文介绍了如何创建与Telit BLE模块的终端I/O连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为android平板电脑编写一个渐进式Web应用程序,该应用程序应该能够通过BLE连接读写具有嵌入式Telit BLE模块的设备.

I'm writing a progressive web app for an android tablet which should be able to read and write to a device with an embedded Telit BLE module via a BLE connection.

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

I am able to open a BLE connection and discover services and characteristics. I am unable to establish a connection over BLE using Telit's Terminal I/O (TIO) protocol.

我的远程(服务器)设备是Falcom Fox3跟踪器单元.建立连接后,可以从Fox3串行端口读取通知事件.通过使用Telit的android终端应用程序连接到Fox3,已成功测试了此测试:

My remote (server) device is a Falcom Fox3 tracker unit. A notification event can be read from the Fox3 serial port when a connection is established. This has been successfully tested by connecting to the Fox3 using Telit's android terminal app: https://play.google.com/store/apps/details?id=com.telit.tiosample

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

I have put together a short function which should connect via BLE, establish a TIO connection and request UART credits before listening for incoming data from the server.

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

I have based my code on a simple script from Smashing Magazine : https://www.smashingmagazine.com/2019/02/introduction-to-webbluetooth/

以下是启动TIO连接的过程,这是《 Telit的终端I/O配置文件客户端实施指南》中给出的.

The process of initiating the TIO connection follows is that given in Telit's Terminal I/O Profile Client Implementation Guide .

终端I/O连接设置包括以下步骤:

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

  • 终端I/O客户端扫描宣传终端I/O服务的低功耗蓝牙设备.
  • 终端I/O客户端与检测到的终端I/O服务器建立了蓝牙低功耗GATT连接.
  • 终端I/O客户端在终端I/O服务器上执行服务发现.
  • 对于检索到的终端I/O服务,终端I/O客户端执行特征发现.
  • 终端I/O客户端订阅UART积分TX特性的指示(请参见7.4).
  • 终端I/O客户端订阅UART数据TX特性的通知(请参见7.2).
  • 终端I/O客户端将初始UART信用传输到服务器(请参阅7.5).
  • 一旦终端I/O客户端收到对传输的UART信用的响应,就认为已建立了终端I/O连接,并应在任何时候期待UART信用TX特性的指示和UART数据特征的通知

连接设置顺序的顺序是强制性的.

The order of the connection setup sequence is mandatory.

我的代码如下,其中log是一种输出到屏幕的功能.

My code is as follows, where log is a function which outputs to the screen.

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设备上建立了蓝牙连接.我希望服务器能够响应连接,触发事件并向其报告.不会发生此类连接事件.

This runs without error and appears to establish a bluetooth connection on my client android device. I expect the server to respond to the connection, trigger an event and report it back. No such connection event occurs.

我对Web蓝牙是陌生的,并且对JavaScript有点生疏,所以不确定我是否使用了正确的调用-特别是对于订阅".如果任何人都可以澄清在这种情况下订阅涉及的内容,那肯定会有助于我的理解.

I am new to web bluetooth and a little rusty with JavaScript, so unsure whether I am using the correct calls - particularly for 'subscribing'. If anyone could clarify what subscribing involves in this context it would certainly help my understanding.

一旦弄清了终端I/O连接说明如何转换为JS,就能够使连接运行.

我执行了以下步骤:对于检索到的终端I/O服务,终端I/O客户端执行特征发现."

I performed these steps like this: "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)

终端I/O客户端订阅了UART积分TX特性的指示(请参阅7.4)."

"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();

终端I/O客户端订阅UART数据TX特性的通知(请参阅7.2)."

"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();

终端I/O客户端将初始UART信用发送到服务器(请参阅7.5)."

"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]))

推荐答案

您可能要等待 writeValue startNotifications .

...
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();

这篇关于如何创建与Telit BLE模块的终端I/O连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆