I am currently developing an embedded system who count the number of cars and saved theirs speed and time. All these data are stored in a simple logs file (thanks to rsyslog). In parallel, I develop an web-API (in Typescript/Angular with Electron for Desktop usage and later Web as well) who allow the user to upload the logs and stored them locally on his laptop.
I setup a GATT server and I'm able to get simple data like battery, and status over Web-Bluetooth, however, can I send/receive over Web-Bluetooth a file ? Or maybe send it in piece by piece ?
I tried the second way, the maximum size is 512 bytes per frame, so I divide my file size by 512 and send X frame(s) to the Web-App but I don't know if it is possible because I can't have something working after a few days ... So then, I found this on Bluetooth's website: https://www.bluetooth.com/specifications/gatt/services/ The 'Object Transfer Service' exist with GATT, but when you click on that we can read: "This service provide management and control features supporting bulk data transfer which occur via a separe L2CAP connection orientel channel". Does that mean we can't send file ?
Should I change my plan and using an protocol ? I also want to send files from laptop to the embedded system, like configuration files and parameters.
I found a solution, I don't know if it is the best one but it is working pretty well! I simply divide my Uint8Array[] in a packet of 512 bytes and send them. Same for writing and reading, below I put my TypeScript code if it can help someone:
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);
}
and the write function:
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);
});
}
Of course, 'this.characteristic' has been saved in my class before I called these functions. Follow https://googlechrome.github.io/samples/web-bluetooth/get-characteristics.html for more information.
I'm a embedded engineer so be fair with my 'ugly web-code' let me know if you have recommendation to optimism this code. Hope it helps.