【问题标题】:Different behavior of chrome on desktop and android mobile while sending data to BLE device向 BLE 设备发送数据时,桌面和 android 移动设备上 chrome 的不同行为
【发布时间】:2021-01-02 00:14:00
【问题描述】:

在我的 ESP32 设备上使用“Web 蓝牙 API”连接到 BLE 服务器时,我遇到了一个奇怪的行为。 ‎
我可以在桌面上使用 chrome 浏览器连接和发送/接收数据,但在我的 android 手机上使用 chrome 浏览器时,我无法将数据发送到设备上的 BLE 服务器。 ‎
我不知道是什么原因。桌面和移动浏览器上的编码/解码是否不同,一些权限问题或 json 字符串以不同的方式发送,或者 移动浏览器发送的数据长度是否存在一些限制,因为我能够发送简单的“hello world” " 来自移动浏览器的字符串,但我无法发送“hello world hello world hello world hello world”?
我的代码如下:‎

    btnconnect.addEventListener('click', submitted);‎
    var wifissid;‎
    var wifipw;‎
    var myjson;‎
    var decoder = new TextDecoder('utf-8');‎
    var encoder = new TextEncoder('utf-8');‎
    var characteristic1;‎
    async function submitted() {‎
    ‎  wifissid = document.getElementById("ssid").value;‎
    ‎  wifipw = document.getElementById("pw").value;‎
    ‎  xdid = document.getElementById("xdid").value;‎
    ‎  var obj = {‎
    ‎    ssid: wifissid,‎
    ‎    pw: wifipw,‎
    ‎    did: xdid,‎
    ‎  };‎
    
    ‎  myjson = JSON.stringify(obj);‎
    
    ‎  try {‎
    ‎    const device = await navigator.bluetooth.requestDevice({‎
    ‎      filters: [{‎
    ‎        name: 'DEVICENAME'‎
    ‎      }],‎
    ‎      optionalServices: [‎‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’‎]‎
                
    ‎    });‎
    ‎    const server = await device.gatt.connect();‎
    ‎    const service = await server.getPrimaryService(‎‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’‎); ‎
    ‎    characteristic1 = await service.getCharacteristic(‘yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy’);‎
    ‎    await characteristic1.startNotifications();‎
    ‎    characteristic1.addEventListener('characteristicvaluechanged',‎
    ‎      handleNotifications);‎
    ‎    let valuesent = encoder.encode(myjson);‎
    ‎    await characteristic1.writeValue(valuesent);‎
    ‎    ‎
    ‎  } catch (error) {‎
    ‎    console.log( error);‎
    ‎  }‎
    
    ‎}‎
    async function handleNotifications(event) {‎
    ‎  let value = await event.target.value;‎
    ‎  let valDecoded = decoder.decode(value);‎
    ‎  if (valDecoded.includes("success")) {‎
    ‎  // Do something
    ‎  }‎
    ‎  if (valDecoded.includes("failed")) {‎
       // Do something
    ‎  }}‎

【问题讨论】:

标签: bluetooth-lowenergy web-bluetooth android-chrome


【解决方案1】:

抱歉,回复晚了。是的,尺寸很重要。对于移动设备,最大传输单元 (MTU) 限制为 23 个字节,但实际上您只能发送 20 个字节。有办法解决这个问题。我通过多次写入特征解决了我的问题。

【讨论】:

  • 你能发布你的代码的sn-p吗?您还可以发布您正在编写的数据样本吗?我遇到了类似的问题,我尝试写了多次,但我收到 GATT 错误未知。
  • 我也有同样的问题。在 Android 上使用 chrome://bluetooth-internals/,我只能提取 20 个字节。当我从 Windows 或 Linux 尝试时,我得到了完整的输出(~200 字节)。但有趣的是,当我在 windows/Linux 之后回到 Android 时,Android 得到了完整的输出。总之,Android 不限于 20 字节,它似乎只是接受来自设备的 MTU(默认为 20)并且不会协商更高的速率。 - 我只是把它作为一种解决方法的可能性放在那里。
猜你喜欢
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 2021-12-13
相关资源
最近更新 更多