https://github.com/sandeepmistry/noble
https://github.com/sandeepmistry/bleno
These are pretty darn good libraries for Node.js (JavaScript) that allow me to write BLE clients and servers that run on my PC (Windows or Linux) that can interact with an ESP32. The documentation isn't brilliant so I'm writing up some guides that suit me ... however the documentation is just about usable.
Here is a sample app that finds my ESP32 device, retrieves a service, retrieves a characteristic, reads the current value of the characteristic and writes a new one.
Code: Select all
var noble = require("noble");
const name = "MYDEVICE";
const ESP32_SERVICE = "4fafc2001fb5459e8fccc5c9c331914b";
const ESP32_CHARACTERISTIC = "4fafc2011fb5459e8fccc5c9c331914b";
noble.on("discover", function(peripheral) {
if (peripheral.advertisement.localName == name ) {
noble.stopScanning();
peripheral.connect((error) => {
peripheral.discoverServices([ESP32_SERVICE], (error, services) => {
if (services.length > 0) {
services[0].discoverCharacteristics([ESP32_CHARACTERISTIC], (error, characteristics) => {
if (characteristics.length > 0) {
var characteristic = characteristics[0];
characteristic.read((error, data) => {
console.log("Here is the current value: " + data);
var newValue = "Hello world: " + new Date().getTime();
console.log("Changing to: " + newValue);
characteristic.write(Buffer.from(newValue));
peripheral.disconnect();
});
}
});
}
});
});
}
});
noble.on("stateChange", function(state) {
if (state == "poweredOn") {
noble.startScanning();
}
});
I wanted to pass this on in case you needed BLE interactions/testing from your PC <-> ESP32.