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

Javascript 我可以通过网络蓝牙发送文件吗?,javascript,typescript,bluetooth-lowenergy,bluetooth-gatt,web-bluetooth,Javascript,Typescript,Bluetooth Lowenergy,Bluetooth Gatt,Web Bluetooth,我目前正在开发一个嵌入式系统,它可以计算汽车的数量,节省他们的速度和时间。所有这些数据都存储在一个简单的日志文件中(感谢rsyslog)。同时,我开发了一个web API(Typescript/Angular和Electron,用于台式机和以后的web),允许用户上传日志并将其本地存储在笔记本电脑上 我设置了一个GATT服务器,可以通过网络蓝牙获取电池等简单数据和状态,但是,我可以通过网络蓝牙发送/接收文件吗?或者一件一件地寄 我尝试了第二种方法,最大大小是每帧512字节,所以我将文件大小除以5

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

我设置了一个GATT服务器,可以通过网络蓝牙获取电池等简单数据和状态,但是,我可以通过网络蓝牙发送/接收文件吗?或者一件一件地寄

我尝试了第二种方法,最大大小是每帧512字节,所以我将文件大小除以512,然后发送X帧到Web应用程序,但我不知道这是否可行,因为几天后我无法让某些内容工作。。。于是,我在Bluetooth的网站上找到了: GATT中存在“对象传输服务”,但当您点击该服务时,我们可以看到:“该服务提供管理和控制功能,支持通过separe L2CAP连接定向通道进行的批量数据传输”。这是否意味着我们不能发送文件


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

我找到了一个解决方案,我不知道它是否是最好的,但它工作得很好! 我只需将Uint8Array[]划分为512字节的数据包并发送它们。写作和阅读也是一样,下面我把我的打字脚本代码放在下面,如果它能帮助别人的话:

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);
}
以及写入功能:

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);
      });
}
等待(ms:number){
var start=new Date().getTime();
var结束=开始;
同时(结束<开始+毫秒){
end=新日期().getTime();
}
}
pushFile(){
让file=this.getFileContent();
let值:字符串;
让valueBytes=null;
console.log(文件);
而(file.length>0){
//复制前512字节
value=file.substr(0,512);
//删除前512字节
调度=file.substr(512)
console.log(文件);
valueBytes=新的TextEncoder().encode(值);
console.log(valueBytes);
const promise=this.characteristic.writeValue(valueBytes);
承诺。然后(val=>{
控制台日志(val);
});
//等待不是强制性的,但以防我的嵌入式系统很忙
这个。等等(100);
}
//发送空帧通知嵌入式系统其传输结束
valueBytes=新的TextEncoder().encode(“”);
const promise=this.characteristic.writeValue(valueBytes);
承诺。然后(val=>{
控制台日志(val);
});
}
当然,“this.characteristic”在调用这些函数之前已经保存在我的类中。请跟随以获取更多信息

我是一名嵌入式工程师,所以请公平对待我的“丑陋的web代码”,如果您有推荐使用此代码的建议,请告诉我。希望能有帮助