首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以通过网络蓝牙发送文件吗?

我可以通过网络蓝牙发送文件吗?
EN

Stack Overflow用户
提问于 2019-06-06 07:16:13
回答 1查看 1.5K关注 0票数 2

我目前正在开发一个嵌入式系统,它可以统计汽车的数量,并节省它们的速度和时间。所有这些数据都存储在一个简单的日志文件中(感谢rsyslog)。同时,我开发了一个web-API ( Typescript/Angular版本,使用Electron for Desktop和后来的Web ),它允许用户上传日志并将其本地存储在笔记本电脑上。

我设置了一个GATT服务器,我可以通过Web-Bluetooth获得简单的数据,比如电池和状态,但是,我可以通过Web-Bluetooth发送/接收文件吗?或者把它一块一块地寄出去?

我尝试了第二种方法,最大大小是每帧512字节,所以我将文件大小除以512,然后将X帧发送到Web-App,但我不知道这是否可行,因为几天后我就不能正常工作了……于是,我在蓝牙的网站上找到了这个:https://www.bluetooth.com/specifications/gatt/services/ 'Object Transfer Service‘与关贸总协定一起存在,但当你点击它时,我们可以看到:“该服务提供了支持批量数据传输的管理和控制功能,这是通过一个单独的面向连接的通道实现的”。这是不是意味着我们不能发送文件?

我应该改变我的计划并使用协议吗?我还想将文件从笔记本电脑发送到嵌入式系统,如配置文件和参数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-07 12:59:55

我找到了一个解决方案,我不知道它是不是最好的,但它工作得很好!我只是将我的Uint8 Array[]分成一个512字节的数据包,然后发送它们。对于写和读也是一样的,下面我把我的TypeScript代码放在下面,如果它对某人有帮助的话:

代码语言:javascript
复制
async getFile() {
    let file: string = '';
    let value: string = 'begin';
    let returnedValue = null;

    while (value != '') {
      try {
        returnedValue = await this.characteristicScheduling.readValue();
        value = String.fromCharCode.apply(null, new Uint8Array(returnedValue.buffer));
        console.log(value);
        file= file.concat(value);

      } catch(e) {
        console.log(e);
      }
    }

    console.log('file: ' + file);
}

和write函数:

代码语言:javascript
复制
wait(ms: number) {
    var start = new Date().getTime();
    var end = start;
    while(end < start + ms) {
      end = new Date().getTime();
    }
}

pushFile() {
    let file= this.getFileContent();
    let value: string;
    let valueBytes = null;
    console.log(file);

    while (file.length > 0) {
      // Copy the first 512 bytes
      value = file.substr(0, 512);
      // Remove the first 512 bytes
      scheduling = file.substr(512)
      console.log(file);
      valueBytes = new TextEncoder().encode(value);

      console.log(valueBytes);
      const promise = this.characteristic.writeValue(valueBytes);
      promise.then(val => {
        console.log(val);
      });
      // The wait is isn't mandatory .. but just in case my embedded system is very busy
      this.wait(100);
    }

    // Send empty frame to notice the Embedded system than its the end of transmission
    valueBytes = new TextEncoder().encode('');
    const promise = this.characteristic.writeValue(valueBytes);
      promise.then(val => {
        console.log(val);
      });
}

当然,在我调用这些函数之前,'this.characteristic‘已经保存在我的类中了。请关注https://googlechrome.github.io/samples/web-bluetooth/get-characteristics.html了解更多信息。

我是一个嵌入式工程师,所以公平地说,我的‘丑陋的网络代码’让我知道,如果你有建议乐观这段代码。希望能有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56469188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档