The code
I couldn't get any of the Bluetooth libraries I found to work (node-bluetooth, noble) so I went with a worse solution: PowerShell. Since I just wanted to send serial data to the Arduino, I just needed the serial port for the Bluetooth module.
#bt_script ps1
$port= new-Object System.IO.Ports.SerialPort COM4
$port.open()
$port.WriteLine("1")
$port.Close()
Then, in whatever Node or Electron app:
const { exec } = require("child_process");
exec(`./bt_script.ps1`, {shell: 'powershell.exe'}, (err, stdout, stderr) => {
console.log(err, stdout, stderr);
});
You should probably have better error handling.
Finding the right serial port
You have to know which serial port the Bluetooth module is connected to.
First, connect your Bluetooth device.
On Windows:
- Open "Bluetooth and other devices settings"
- Click on "More Bluetooth options" on the right
- Click on the "COM Ports" tab (the middle one)
- To send data to the Bluetooth module, you want the Outgoing port for your device. In the example above, that's COM4.
- If you want to listen for data from the Bluetooth module, you want the Incoming port for your device (I haven't done this, so I don't know how that works.)
On other platforms:
- I don't know, I've only done this on Windows.