Emill / node-ble-host

A Bluetooth Low Energy host implementation for Node.js
ISC License
55 stars 15 forks source link

conn.l2capCoCManager.registerLePsm return undefined #4

Closed sialalex closed 2 years ago

sialalex commented 2 years ago

Hello!

I am trying to implement an BLE L2CAP COC Server Socket on a RPi.

I used the code samples, but I always get "undefined" when calling "conn.l2capCoCManager.registerLePsm".

const HciSocket = require('hci-socket');
const NodeBleHost = require('ble-host');
const BleManager = NodeBleHost.BleManager;
const AdvertisingDataBuilder = NodeBleHost.AdvertisingDataBuilder;
const HciErrors = NodeBleHost.HciErrors;
const AttErrors = NodeBleHost.AttErrors;
const L2CAPCoCErrors = NodeBleHost.L2CAPCoCErrors;

const deviceName = 'RaceOn3';

var transport = new HciSocket(); // connects to the first hci device on the computer, for example hci0

var options = {
    // optional properties go here
};

BleManager.create(transport, options, function (err, manager) {
    // err is either null or an Error object
    // if err is null, manager contains a fully initialized BleManager object
    if (err) {
        console.error(err);
        return;
    }

    var notificationCharacteristic;

    manager.gattDb.setDeviceName(deviceName);

    const advDataBuffer = new AdvertisingDataBuilder()
        .addFlags(['leGeneralDiscoverableMode', 'brEdrNotSupported'])
        .addLocalName(/*isComplete*/ true, deviceName)
        .add128BitServiceUUIDs(/*isComplete*/ true, ['71ced1ac-0000-44f5-9454-806ff70b3e02'])
        .build();
    manager.setAdvertisingData(advDataBuffer);
    // call manager.setScanResponseData(...) if scan response data is desired too
    startAdv();

    function startAdv() {
        manager.startAdvertising({/*options*/ }, connectCallback);
    }

    function connectCallback(status, conn) {
        if (status != HciErrors.SUCCESS) {
            // Advertising could not be started for some controller-specific reason, try again after 10 seconds
            setTimeout(startAdv, 10000);
            return;
        }
        conn.on('disconnect', startAdv); // restart advertising after disconnect
        console.log('Connection established!', conn);

        // 0x0001 - 0x007f for fixed Bluetooth SIG-defined services, 0x0080 - 0x00ff for custom ones.
        const lePsm = 0x0080;

    console.log("Wkroing till here...");

        var a = conn.l2capCoCManager.registerLePsm(lePsm, function onRequestCallback(txMtu, callback) {
            // txMtu is the maximum packet (SDU) size the peer can receive,
            // which means we should never send a packet larger than this.

        console.log("Working till here..");
            // If we would reject, for example due to the reason we can only handle one l2cap connection at a time
            // callback(L2CAPCoCErrors.NO_RESOURCES_AVAILABLE);

            // Accept, enable receiving of packets immediately, with maximum receive size of 65535 bytes per packet
            var l2capCoC = callback(L2CAPCoCErrors.CONNECTION_SUCCESSFUL, /*initiallyPaused*/ false, /*rxMtu*/ 65535);

        console.log(l2capCoC.txCredits);

            // Receiving packets
            l2capCoC.on('data', function (buffer) {
                console.log(buffer);
            });

        });
    console.log(a);
    console.log("Something didnt worked");
    }
});
Emill commented 2 years ago

Hi. The registerLePsm method does not return anything, hence you see undefined if you print the return value. Instead, when the remote device makes a connection to this psm, that inner callback will be called.

sialalex commented 2 years ago

Hi!

Thank you very much for the fast response! I have an additional question - the sequence of my code is right? I mean that a register the the LE PSM after a successfull GATT Connection? Or should i change something? Maybe to register the LE PSM before...

Thank you in andvance!

Emill commented 2 years ago

Your code uses the perfectly correct sequence. You register psm on an established connection, not before.