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

可紊 提交于 2021-01-01 04:12:36

问题


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.

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.

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

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.

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

The process of initiating the TIO connection follows is that given in Telit's 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.

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

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.

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.

Edit: I was able to get the connection running once I figured out how the Terminal I/O connection instructions translate to JS.

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)

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

回答1:


You may want to await writeValue and 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();


来源:https://stackoverflow.com/questions/55374928/how-does-one-create-a-terminal-i-o-connection-to-a-telit-ble-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!