Close

Finally sent data to the device!

A project log for Open source hackable smart watch with app

Develop an open source simple smart watch with companion smart phone app that can be used by others to make their own

jeff-vJeff V 09/24/2024 at 04:580 Comments

It took a ludicrously long time to finally get to a consensus between my device and my React Native app, but I finally sent meaningful data from the app to the device and it was received correctly.

On the device, I used the BLEDevice library among other things, and utilized the Arduino example for BLE -> Write.

However, I was left with an issue: how exactly do I send data over to the device from an app? I wanted to stay in React Native if possible.

The react-native-ble-plx library was what I was using, and I managed to get to the point where it was the Profile.Service.Characteristic I had to write to. But that still left the matter of how to package the data and send it.

  const sendData = (str) => {
    // THIS IS THE MAGIC SAUCE THAT ENABLES A STRING TO BE SENT TO ARDUINO ESP32 WITH BLE USING BLEDEVICE LIBRARY
    const myBuffer = Buffer.from(str).toString("base64");

    connectedDevice
      .writeCharacteristicWithResponseForService(
        SERVICE_UUID,
        CHARACTERISTIC_UUID,
        myBuffer,
      )
      .catch((e) => {
        console.log("Failed to sendData", JSON.stringify(e));
      });
  };

 As you can see, the code to write to the characteristic was fairly boilerplate, but I didn't know how to send the string to the device so that it would come out decipherable on the other end.

After an agonizing 12 hours or so of research and trial and error, I went from "Buffer.from(str)" to "Buffer.from(str).toString("base64"), and it went over cleanly.

There is a bit of a glitch where if the device doesn't initialize properly, it will throw an error on the app side, but after resetting, it seemed to work without issue.

Here are the repositories for the app and the watch. It's mostly copy and paste code anyway, so I certainly don't mind sharing.

App

Watch

I will need to update my objectives, but this was a huge blocker that I finally managed to clear.

I hate to make it sound more grandiose than it is, but imagine first being able to send data over a network, before the Internet. It kinda felt like that.

Discussions