Open shohamrot opened 5 years ago
Same problem here, but I'm trying to use it as a "client" (basically reproducing the example provided in the sources). "Serial Connection from" is never printed.
I'm doing something wrong?
const Bluez = require('bluez');
const bluetooth = new Bluez();
// Register callback for new devices
bluetooth.on('device', (address, props) => {
// apply some filtering
if (props.Name !== "ESP32_LED_Control") return;
setTimeout(
() => connectToDevice(address, props).catch(console.error),
1000
);
});
bluetooth.init().then(async () => {
// Register Agent that accepts everything and uses key 1234
await bluetooth.registerDummyAgent();
console.log("Agent registered");
// Register a Serial Client Service
var t = await bluetooth.registerSerialProfile(async (device, socket) => {
const name = await device.Name();
console.log("Serial Connection from " + name);
// socket is a non blocking duplex stream similar to net.Socket
// Print everything
socket.pipe(process.stdout);
//socket.on('data', (data)=>process.stdout.write(data));
socket.on('error', console.error);
// Send hello to device
socket.write("Hello\n");
}, "client");
console.log(t);
console.log("SerialProfile registered");
// listen on first bluetooth adapter
const adapter = await bluetooth.getAdapter('hci0');
await adapter.StartDiscovery();
console.log("Discovering");
}).catch(console.error);
async function connectToDevice(address, props) {
console.log("Connecting to " + address + " " + props.Name);
// Get the device interface
const device = await bluetooth.getDevice(address);
// Pair with the device if not already done
// Note: pairing twice will throw an error
if (!props.Paired) {
await device.Pair().catch((err) => {
console.error("Error while pairing to device " + address + ": " + err.message);
});
}
// Connect to the Serial Service
await device.ConnectProfile(Bluez.SerialProfile.uuid).catch((err) => {
console.error("Error while connecting to device " + address + ": " + err.message);
});
console.log("Connected");
}
Your code looks good, what is your Bluez version? Does "Connected" get printed without error message? What version are you using 0.3.3 or current master (0.4.0 but not released yet)?
Thanks for your reply. I'm using Bluez version 5.53-0ubuntu3. I try version 0.3.3 from npm and also version 0.4.0. In both versions, the result is the same: I can connect to the device, but the serial callback is never called:
Agent registered
SerialProfile registered
Discovering
Connecting to 3C:71:BF:FE:71:0E ESP32_LED_Control
Connected
Does your device export a Serial Port Service?
bluetoothctl info <address>
it should list UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)
Make sure that there are no other applications running that create a Serial Profile otherwise those might receive the connection instead.
Yes, it does. The output of bluetoothctl
command is:
Device 3C:71:BF:FE:71:0E (public)
Name: ESP32_LED_Control
Alias: ESP32_LED_Control
Class: 0x0002c110
Icon: computer
Paired: yes
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Serial Port (00001101-0000-1000-8000-00805f9b34fb)
And I can connect to it using Python, for instance.
It took me a while but I tested it on:
I can not reproduce this issue. It must be something device specific. Some thoughts I have:
btmon
log of the connection event and check the difference between python and bluezOne more thing to check:
dbus-monitor
and see where its goingThis is what I am getting when I register a serial profile as "server".
bluetoothd[577]: src/profile.c:create_ext() Invalid value for profile option PSM
bluetoothd[577]: src/profile.c:ext_start_servers() RFCOMM server failed for Node Serial Port: rfcomm_bind: Address already in use (98)
If I use "client" then I just get bluetoothd[577]: src/profile.c:create_ext() Invalid value for profile option PSM
Haven't quite figured out where and what PSM is just yet, and still digging into the RFCOMM bind address. Funny thing is there is no /dev/rfcomm0
Got fed up with troubleshooting and getting the same failed result. Set up a VM with Debian 12. Installed all the required dependencies. Python was a PITA for node-gyp. Started up my app and the Bluetooth serial connection worked without issue. I can send and receive data without issue. Tested using the Android Bluetooth Serial app.
Since the change in the underlying dbus dependency and using the registerDummyAgent I'm unable to detect a serial connection. I'm using a raspberry pi as a "server".
This is my code:
The handleSocketConnection function starts like this:
"Serial Connection from" is never printed.