Niels-Be / node-bluez

Node.js Bluez5 D-Bus library
MIT License
40 stars 23 forks source link

Serial connection issue #17

Open shohamrot opened 5 years ago

shohamrot commented 5 years ago

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:

bluetooth.on('device', async (address, props) => {
        const device = await bluetooth.getDevice(address);
        logger.info("Found new Device " + address +" "+props.Paired+" "+props.Connected+" "+props.Name+ " ")
        if(props.Paired){
          logger.debug("paired device "+address)
        }
        else{
          logger.debug("unpaired device", address )
        }

    });

    bluetooth.init().then(async ()=>{

        // Register Agent that accepts everything and uses key 1234
        const agentResult = await bluetooth.registerDummyAgent();
        logger.info("Agent registered",agentResult);

        // Register a Serial Client Service
        const result = await bluetooth.registerSerialProfile(this.handleSocketConnection.bind(this), "server");
        logger.info("SerialProfile registered",result);

        // listen on first bluetooth adapter
        const adapter = await bluetooth.getAdapter('hci0');
        adapter.DiscoverableTimeout(config.bluetooth.discoverTimeOut)
        await adapter.Discoverable('on')

        await adapter.Pairable('on')

    });

The handleSocketConnection function starts like this:

  async handleSocketConnection(device,socket){
    const name = await device.Name();
    logger.info("Serial Connection from " + name);

"Serial Connection from" is never printed.

jordanorc commented 4 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");
}
Niels-Be commented 4 years ago

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)?

jordanorc commented 4 years ago

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
Niels-Be commented 4 years ago

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.

jordanorc commented 4 years ago

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.

Niels-Be commented 4 years ago

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:

Niels-Be commented 4 years ago

One more thing to check:

fromage9747 commented 7 months ago

This 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

fromage9747 commented 7 months ago

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.