我找到了一个解决方案,我不知道它是否是最好的,但它工作得很好!我只是将我的 Uint8Array[] 分成一个 512 字节的数据包并发送它们。写作和阅读也是如此,如果它可以帮助某人,我将我的 TypeScript 代码放在下面:
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);
});
}
当然,在我调用这些函数之前,'this.characteristic' 已经保存在我的课堂上。关注https://googlechrome.github.io/samples/web-bluetooth/get-characteristics.html了解更多信息。
我是一名嵌入式工程师,所以请公平对待我的“丑陋的网络代码”,如果您有建议看好这段代码,请告诉我。希望能帮助到你。