我正在为android平板电脑编写一个进步的web应用程序,它应该能够通过BLE连接读写带有嵌入式Telit模块的设备。
我能够打开一个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信用。
我的代码基于“粉碎杂志”中的一个简单脚本:https://www.smashingmagazine.com/2019/02/introduction-to-webbluetooth/
启动TIO连接的过程如下所示,在Telit的终端I/O Profile客户端实现指南中给出了这个过程。
终端I/O连接设置由以下步骤组成:
连接设置序列的顺序是强制性的。
我的代码如下所示,其中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.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();终端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]))发布于 2019-03-28 06:23:21
您可能希望等待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();https://stackoverflow.com/questions/55374928
复制相似问题