![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
bluetooth-serial-port
Advanced tools
This piece of open source software is free (as in speech) to use, modify and distribute, no strings attached. If this software saves you some cash and you want to do something for the good please consider making a donation to a charity. Any charity will do. 🙏
MacOS support has (temporarily) been dropped as of version v3.0.0 of the module. If you use the module on a Mac please stay on v2 of this module.
DEPRECATED Currently I have no plans to add support for NodeJS version 1.13 and up. If you want to help out by adding support please contact me. It was a great experience maintaining this project for almost 8 years. It was great to see people step in to improve this project. Thank you all!
This node module lets you communicate over Bluetooth serial port with devices using NodeJS. The goal is have an easy to use API. This module is great for communicating with Bluetooth enabled Arduino devices.
If you have any problems make sure to checkout the FAQ.
lts
in the DockerfileCheck the release notes for an overview of the change history.
apt-get install build-essential libbluetooth-dev
As the initial implementation of the RFCOMM server sockets is based on BlueZ4, in order to work with SDP we need to change the bluetooth service configuration file by modifing the systemd unit file: bluetooth.service:
(Debian based distro)
sudo vim /lib/systemd/system/bluetooth.service
(RedHat based distro)
sudo vim /usr/lib/systemd/system/bluetooth.service
and adding the --compat flag to the ExecStart value:
ExecStart=/usr/lib/bluetooth/bluetoothd
--compat
Finally, restart the service:
sudo systemctl daemon-reload
sudo systemctl restart bluetooth
npm install bluetooth-serial-port
docker build -t bluetooth-serial-port .
var btSerial = new (require("bluetooth-serial-port").BluetoothSerialPort)();
btSerial.on("found", function (address, name) {
btSerial.findSerialPortChannel(
address,
function (channel) {
btSerial.connect(
address,
channel,
function () {
console.log("connected");
btSerial.write(
Buffer.from("my data", "utf-8"),
function (err, bytesWritten) {
if (err) console.log(err);
}
);
btSerial.on("data", function (buffer) {
console.log(buffer.toString("utf-8"));
});
},
function () {
console.log("cannot connect");
}
);
// close the connection when you're ready
btSerial.close();
},
function () {
console.log("found nothing");
}
);
});
btSerial.inquire();
var server = new (require("bluetooth-serial-port").BluetoothSerialPortServer)();
var CHANNEL = 10; // My service channel. Defaults to 1 if omitted.
var UUID = "38e851bc-7144-44b4-9cd8-80549c6f2912"; // My own service UUID. Defaults to '1101' if omitted
server.on("data", function (buffer) {
console.log("Received data from client: " + buffer);
// ...
console.log("Sending data to the client");
server.write(Buffer.from("..."), function (err, bytesWritten) {
if (err) {
console.log("Error!");
} else {
console.log("Send " + bytesWritten + " to the client!");
}
});
});
server.listen(
function (clientAddress) {
console.log("Client: " + clientAddress + " connected!");
},
function (error) {
console.error("Something wrong happened!:" + error);
},
{ uuid: UUID, channel: CHANNEL }
);
Emitted when data is read from the serial port connection.
Emitted when a connection was closed either by the user (i.e. calling close
or remotely).
Emitted when reading from the serial port connection results in an error. The connection is closed.
Emitted when a bluetooth device was found.
Emitted when the device inquiry execution did finish.
Starts searching for bluetooth devices. When a device is found a 'found' event will be emitted.
Starts searching synchronously for bluetooth devices. When a device is found a 'found' event will be emitted.
Checks if a device has a serial port service running and if it is found it passes the channel id to use for the RFCOMM connection.
callback(channel) - called when finished looking for a serial port on the device.
errorCallback - called the search finished but no serial port channel was found on the device. Connects to a remote bluetooth device.
bluetoothAddress - the address of the remote Bluetooth device.
channel - the channel to connect to.
[successCallback] - called when a connection has been established.
[errorCallback(err)] - called when the connection attempt results in an error. The parameter is an Error object.
Closes the connection.
Check whether the connection is open or not.
Writes a Buffer to the serial port connection.
err
parameter is set an error has occured, in that case err
is an Error object. When err
is not set the write action was successful and bytesWritten
contains the amount of bytes that is written to the connection.NOT AVAILABLE ON LINUX
Lists the devices that are currently paired with the host.
pairedDevices
object.Listens for an incoming bluetooth connection. It will automatically advertise the server via SDP
callback(address) - is called when a new client is connecting.
errorCallback(err) - is called when an error occurs.
options - An object with these properties:
uuid - [String] The UUID of the server. If omitted the default value will be 1101 (corresponding to Serial Port Profile UUID). Can be a 16 bit or 32 bit UUID.
channel - [Number] The RFCOMM channel the server is listening on, in the range of 1-30. If omitted the default value will be 1.
Example:
var options = { uuid: 'ffffffff-ffff-ffff-ffff-fffffffffff1', channel: 10 }
Writes data from a buffer to a connection.
error
contains the error is appropriated. len
has the number of bytes that were written to the connection.Stops the server.
Disconnects the currently-connected client and re-listens and re-publishes to SDP.
Checks is a server is listening or not.
Emitted when data is read from the serial port connection.
Emitted when a connection was disconnected (i.e. from calling disconnectClient
or if the bluetooth device disconnects (turned off or goes out of range)).
Emitted when the server is closed (i.e. from calling close
or as the result of a non-disconnect error).
Emitted when reading from the serial port connection results in an error. The connection is closed.
The type script declaration file is bundled with this module.
import * as btSerial from "bluetooth-serial-port";
btSerial.findSerialPortChannel(address: string, (channel: number) => {
btSerial.connect(address: string, channel: number, () => {
btSerial.write(Buffer.from("yes"), (err) => {
if (err) {
console.error(err);
}
});
}, (err?: Error) => {
if (err) {
console.error(err);
}
});
btSerial.on("data", (buffer: Buffer) => console.log(buffer.toString("ascii")));
}, () => {
console.error("Cannot find channel!");
});
This module is available under a FreeBSD license, see the LICENSE file for details.
FAQs
Bluetooth serial port communication for Node.js
The npm package bluetooth-serial-port receives a total of 141 weekly downloads. As such, bluetooth-serial-port popularity was classified as not popular.
We found that bluetooth-serial-port demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.