0

For a group project I was trying to use the Arduino BLE to connect to my computer, which would be connected to a website that would then return an appropriate value to the Arduino BLE. The issue occurs after connecting to the BLE, because my website showed basically no signs of responding to my BLE's messages at all. After a bit of testing and logging, I found that the issue was that I was using an eventListener in my javascript to detect whenever the BLE sends a message, but that whenever that message was sent, it does not detect anything because the SerialCharacteristic does not change.

I tried changing some random stuff, from changing the ports of the BLE, to readjusting how I'm sending the strings, but I have no clue how to change the SerialCharacteristic. When I tried using the bt.write() function, it said that apparently bt.write() function didn't exist. I asked my friend who had used the BLE before and they said that bt.print() worked for them, so I really don't know why the SerialCharacteristic refuses to change. I can tell that the BLE is connected because of logs, but otherwise I have no clue why things are malfunctioning. Also, during testing there was one random time when the message did get through, but I wasn't logging stuff at the time and I failed to recreate it. Relevant JS code:

const serviceUUID = 0xFFE0;
const serialUUID = 0xFFE1;

let device;
let serialCharacteristic;

async function connect(){

    device = await navigator.bluetooth.requestDevice({
        filters: [{
            services: [serviceUUID]
        }],
    });
    console.log("before server");
    const server = await device.gatt.connect();
    const service = await server.getPrimaryService(serviceUUID);

    serialCharacteristic = await service.getCharacteristic(serialUUID);
    console.log(serialCharacteristic);
    await serialCharacteristic.startNotifications();
    console.log("connected");
    serialCharacteristic.addEventListener('characteristicvaluechanged', read);

    document.getElementById('connect').removeEventListener("click", connect);
    document.getElementById('connect').addEventListener("click", disconnect);
    document.getElementById('connect').textContent = "Disconnect";
}

Relevant Arduino Code :

#include <SoftwareSerial.h>

String screen;
String btdata;
String serialdata;
SoftwareSerial bt(10, 11);

void setup() {
    bt.begin(9600);
}
void loop() {
    ...//irrelevant code
    Serial.println(screen)
    bt.print(screen);
       
    while (bt.available() == 0) { //I think bt.available() returns the number of characters ready to be read, so essentially I'm waiting until there's something being returned
        Serial.println(bt.available());
        delay(100);
    }
    btdata = bt.read();
    Serial.println(btdata);
}

3
  • Are you sure you are using BLE on the Arduino? Or are you using something like an HC-05 with is Bluetooth Classic? Try debugging your Arduino setup by using a Bluetooth Low Energy scanning and exploration tool such as nRF Connect. Once you are confident the Arduino is correct then work on the JS code. Commented Apr 25, 2024 at 7:20
  • Thank you kind stranger; I was working with an HC-06, I assumed it was a BLE because I borrowed this component from a friend and they told me it was BLE. That might be why it cannot send any messages to the computer. The only thing that's bugging me is when the thing magically connected somehow, but I'll either try a different approach or actually get a BLE module. Commented Apr 25, 2024 at 21:04
  • The HC-06 module is based on the CSR BC4 chip, which is old and only Bluetooth V2.0 + EDR. (Not BLE). The HM-10 is the only similar module that I know that supports BLE. Commented Apr 26, 2024 at 5:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.