Is it possible to transfer files using web-bluetooth technology?

As I work on developing an embedded system that counts the number of cars, saves their speed and time data in a logs file using rsyslog. Simultaneously, I am creating a web-API (in Typescript/Angular with Electron for Desktop usage and later Web as well) that allows users to upload these logs and store them locally on their laptops.

I have successfully set up a GATT server and can acquire simple data like battery level and status over Web-Bluetooth. However, I am unsure if it is possible to send/receive a file using Web-Bluetooth or perhaps send it piece by piece.

My attempt at sending the file in pieces involved dividing the file size by 512 bytes per frame and transmitting X frame(s) to the Web-App. Unfortunately, I encountered issues after a few days of trying this method. This led me to discover information on Bluetooth's website about the 'Object Transfer Service' available with GATT. However, upon further investigation, it appears that this service primarily supports bulk data transfer via a separate L2CAP connection orientel channel, which raised questions about the possibility of sending files.

Considering the challenges faced with Web-Bluetooth, I am contemplating whether it would be more feasible to modify my plan and utilize a different protocol for transferring files between the laptop and the embedded system, specifically for tasks such as sending configuration files and parameters.

Answer №1

After experimenting with different approaches, I've come up with a solution that seems to work quite effectively. Essentially, I'm dividing my Uint8Array[] into packets of 512 bytes for transmission, writing, and reading. Below is the TypeScript code I've used:

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 here's 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 notify the Embedded system that it's the end of transmission
    valueBytes = new TextEncoder().encode('');
    const promise = this.characteristic.writeValue(valueBytes);
      promise.then(val => {
        console.log(val);
      });
}

For more information on Web Bluetooth characteristics, refer to this link.

As an embedded engineer, my web-coding skills may not be perfect. Feel free to provide recommendations on how to optimize this code. I hope this code snippet proves helpful to others.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to define coordinates or set margins for the autoTable component in jsPdf when using React js?

While working with the jsPdf library to create a PDF in React, I am encountering an issue where I am unable to set margins for the autoTable when there are multiple tables on a single page. Below is the code snippet: import React from "react"; ...

Conceal any elements designated by a particular class and reveal them based on their corresponding ID

I have utilized jQuery's hide() function to initially hide all elements of a specific class when the page loads. My goal is to make individual elements visible again based on their unique IDs when a corresponding link is clicked. In total, there are ...

Sorting array of arrays in TypeScript and Node.js involves defining the arrays and then applying a sorting algorithm

Recently delved into TypeScript with limited JavaScript knowledge just a couple of weeks ago. I am attempting to scan through all the files in a particular directory, gather each file name (string) and modification time (number>), then organize them in ...

Selecting a JSON object at random

My data is stored in JSON format. [ ["Apple","A"], ["Orange","O"], ["Grape","G"], ["Kiwi","K"] ] Is there a way to randomly select an array item from this data (e.g. ["Grape","G"])? var letterNum = ""; $.ajax ( { url:"getletter.json" }).done( ...

Cannot save PDF files to a server using jsPDF

I'm new to this community and still learning javascript & php. I am having trouble saving my PDFs with jsPDF to the local storage on the server (automatically generated). It used to work in the past, but now when I add Canvas (javascript) to my HTML, ...

The Angular JSONP feature is malfunctioning

I'm attempting to utilize a jsonp call with the following code, but it doesn't appear to be functioning as expected. Code var url = 'http://z:15957/Category/Categories?callback=JSON_CALLBACK'; $http.jsonp(url).success(function (data) ...

Discovering how to efficiently track and respond to changes in MobX state within a React

Within my react native project, I have observed that upon clicking a button, the state of my mobx app undergoes an update. To address this issue, I aim to employ a react lifecycle method that can monitor and automatically reflect this modification. For th ...

Steps to resolve the issue: The current experimental syntax 'classProperties' is not supported

I encountered an issue where the experimental syntax 'classProperties' is not supported when trying to run my react js application. My goal is to increment the value inside a <span> when a ` button is clicked. Below is the excerpt of my c ...

A guide to building a versatile component using Ionic 3 and Angular 4

I decided to implement a reusable header for my app. Here's how I went about it: First, I created the component (app-header): app-header.ts: import { Component } from '@angular/core'; @Component({ selector: 'app-header', te ...

When a user connects to Node.js using sockets, the previous messages are loaded. However, a bug causes the messages to be loaded to all chat users, instead of just the

New to node.js, I am currently creating a chat application with two main files: server.js (server side) and script.js (client side). In the server.js file: socket.on('previousMessages', function (data){ db.query("SELECT * FROM messages", f ...

Challenges faced when subscribing to global events in JavaScript

I have some questions about the JavaScript code snippet below: What does .events.slice(-1)[0][0] signify? Similarly, could you explain the purpose of nodes_params += "&ns=" + GLOBAL_EVENT + "," + start_from + ",-,-";? Could you elaborate on what is m ...

"Encountering a glitch in the Typescript/Node API where Express routes

Encountering a peculiar issue here - when attempting to import my router from an external file and add it as a route, I keep getting this unusual error where the string appears to be enclosed in double quotes. https://i.sstatic.net/nm9Wn.png ...

Exiting a void method in JavaScript/Typescript using the return or break statement

I find myself dealing with a complex method in Typescript that contains multiple if else if else constructs. It's a void method, and I'm wondering how I can exit the method based on a specific if condition without having to execute the remaining ...

Dealing with AngularJS: Issue arises when attempting to inject $modal into a controller nested within a directive

Our team has implemented a custom directive that wraps around a checkbox and utilizes transclusion to inject content into it. Here is an example of the setup: somecheckbox.js angular.module('namespace.directives') .directive('someCheckbox& ...

Bootstrap Dropdown Functionality Malfunctioning

Here is a simple piece of HTML code that I have created: <!doctype html> <html> <head> <meta charset="utf-8"> <title>.:Home Page:. The Indian Sentinel</title> <link rel="stylesheet" href=" ...

What is the general consensus on combining SWR with Redux - is it considered a best practice?

I am currently incorporating both SWR and Redux into my code. Here is how I'm using them together: const vehiclesStates = useSelector(({ vehiclesStates: state }) => state); // REDUX const response = useFetch("/vehicles/states") // SWR con ...

Errors in Ionic 6 involving the FormBuilder, FormGroup, Validators, FormControl, and ControlContainer

I am currently working on creating a basic registration form using Ionic 6.12.3 ionic -V, Angular CLI version 11.0.5, and npm version 6.14.11. You can find the repository for this project here: Repo. Below is my implementation for the register.page.ts: // ...

Enforce a restriction on the user's input value for the amount field within a React application

I'm looking to limit the user from entering more than 50000 in the input value. How can I achieve this using React? I am currently handling this on the onchange event. onPaymentAmountChanged = (e) => { let inputValue = e.target.value; if (i ...

What causes images to expand automatically when selected in my JavaScript gallery?

I am struggling with a simple JS gallery of images where I want to display a large image at the top and small thumbnails below it. The issue I am facing is that when I hover over an image in the thumbnail section, the big image changes as expected. However ...

Retrieve data by sorting based on the count column in a joined table with Sequelize

I've been struggling to make this work for some time and was hoping for some guidance. OBJECTIVE: I'm attempting to sort the posts by the number of likes they currently have. CURRENT: const posts = await db.post.findAll({ include: [ db.user ...